I have a jquery that everyone at my site can click on to update their subscription that looks like this
<div id="role-options-<%= current_user.id %>" class="modal modalSubscription" style="display: none;">
<%= simple_form_for current_user, :url => user_path(current_user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
...Form stuff
<div class="modal-footer">
<%= f.submit "Update Settings", :class => "btn btn-green" %>
<a class="btn btn-green closeModal" data-dismiss="modal" href="#">Close</a>
</div>
<% end %>
</div>
And i am trying to push this to a custom route that is an update subscription path. I am using devise. In a perfect world, i would like to send it to a separate controller that is labeled SubscriptionsController so that it can handle the update action. I've tried many things in the routes
#I expected this to send the request to the subscription controller after i changed the :url to update_subscriptions_path, but it did not
put "/users/update_subscription", :to => 'subscription#update'
I also tried just adding an "update_subscription" method in the controller and set the url in the form to
users_update_subscription_path
However this posted to the update path
Alright so was making a mistake, the paths i was adding were after resources :users, it looks like rails sees the put request and automatically assumes that I want to update the user and didn't make it down to the extra route I had added.
Related
A user has the ability to edit their credit card information, like so:
CreditCardsController:
class CreditCardsController < ApplicationController
before_filter :authenticate_user!
respond_to :js
def edit
#cc = current_user.credit_cards.where(:id => params[:id]).first
respond_with #cc
end
end
_form.html.erb:
<%= form_for #cc, :remote => true, :html => { :method => :put } do |f| %>
<div id="cancel-subscription" class="modal-content">
<div class="header dotted-border">
<h2>Billing Information</h2>
<p>Edit the fields below to update your information</p>
</div>
<div class="content dotted-border">
<h2>Credit Card</h2>
</div>
</div>
<% end %>
For some reason the form_foris ignorning the :method option even if I leave it off. It keeps getting set to post. That is not correct since I am editing/updating a CC entry. Anyone else run into this issue?
Just adding
:html => { :method => put }
will not work, because form_for generate _methode hidden element with form on update and delete.
just using
:method => "put"
in form may work
my routes
TerritoryManagement::Application.routes.draw do
get 'new' => 'territories#new', :as => 'new'
root :to => 'territories#index', :as => 'territories'
resources :territories
resources :users
end
create in my controller
def create
#territory = Territory.new(params[:territory])
if #territory.save
redirect_to root_url, :notice => "Product successfully created!"
else
render "new"
end
my view
<%= form_for(#territory) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
this generates
<form accept-charset="UTF-8" action="/" class="new_territory" id="new_territory" method="post">
I know that this action="/" is the problem, but I don't understand why it's being generated? How to modify my routes that the app will create the record and then goes to the index or edit view?
Thanks
Thomas
This worked for me:
TerritoryManagement::Application.routes.draw do
get 'new' => 'territories#new', :as => 'new'
resources :territories
root :to => 'territories#index'
end
It seems that the :as option was causing some issue. That's there to give the route a name, but since you've already done 'resources :territories' you already have named routes for the standard CRUD actions. I also moved the root route to the end of the file. I can't remember why, but it seems like this was a 'Best Practice' back in the Rails 2.3 days.
Using this previous question as a guide, I've attempted to create a ul navigation header above a container that renders partials within the container when clicked. (Hopefully that makes some sense, but it may not be important.) Until the links for the partials are clicked, I have it rendering a partial by default.
However, when I went to click my link_to in hopes of rendering the partial I get the following error:
uninitialized constant ProfileController
I'm using Rails 3. Here's my relevant code:
ProfilesController:
def show_about
#is_on_show_about = true
end
def show_info
#is_on_show_info = true
end
views/profiles/show.html.erb:
<div id="info">
<div id="infoContainer">
<% if #is_on_show_about %>
<%= render :partial => 'show_about' %>
<% elsif #is_on_show_info %>
<%= render :partial => 'show_info' %>
<% end %>
<ul id="info">
<li>
<%= link_to 'About', show_about_path, :remote => true %>
</li>
</ul>
<ul id="settingsLinks">
<li>Advice</li>
<li>
<%= link_to 'Info', show_info_path, :remote => true %>
</li>
</ul>
</div>
<%= render :partial => 'show_about' %>
Routes.rb:
map.show_info 'profiles/:id/info', :controller => 'profile', :action => 'show_info'
map.show_about 'profiles/:id/about', :controller => 'profile', :action => 'show_about'
Can anyone help me fix this and explain what went wrong?
Both of your routes are incorrect.
If your controller is indeed named ProfilesController (plural) then your routes should use :controller => 'profiles', instead of :controller => 'profile'.
I have an edit page with the following code, basically a dropdown and button that calls update.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label>Car Name:</label>
<%= select("params", ":id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
The server reads like the params is being set:
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"8vHXrnICaOKrGns6FfMUcd/dWo5kpNKpA8F5l5ozRkY=", "params"=>{":id"=>"9"}, "commit"=>"Switch Car"}
However, when I put the params into a session I get nothing. It seems to be always nil. Not sure what I am doing wrong? Here is code in the controller.
def update
if params[:id]
session[:car_info_id] = params[:id]
redirect_to entry_url
else
redirect_to switch_car_path
end
end
It always gets redirected to the switch_car_path so I am assuming params[:id] is always nil. When I put if params[:id] == nil it goes to the entry_url.
Thanks in advance.
you want params[:params][":id"]
Alternatively, you could put this in your view:<%= select("car_info", "id", #available_cars.collect {|v| [v.name, v.id]})%>
And then in your controller:if params[:car_info][:id]
While the other answer would work, this is probably what you'd want to be doing (using select_tag(:id) will automatically add an :id key/value to the params hash):
<% form_tag('switch_car', :method => :put) do %>
<div class="field">
<label>Car Name:</label>
<%= select_tag(:id, options_from_collection_for_select(#available_cars, "id", "name")) %>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
Then you can easily access params[:id] in the controller.
I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>