If I defined in the routes.rb
map.resources :cars
Then in my view, if I define a form like following;
<% form_for :car, #car, :url => cars_url(#car), :html => {:method => :put} do |form|%>
...
<% end %>
When I submit the form, I notice that the update method inside cars_controller.rb is executed.
Is it because of the HTML "PUT" verb defined in :html => {:method => :put} in the form_form which hints the rails to run update method in controller ?
Yes. This is a standard RESTful practice.
The idea is that every car has a unique id. if you "put" to that id - then you are "putting new information in that car object".
Related
I have this in my routes file:
resources :payments do
member do
post :add_payment
end
end
With rake routes looking like this:
add_payment_payment POST /payments/:id/add_payment(.:format) payments#add_payment
And my form_for tag has this:
<%= form_for #loan, :url => add_payment_payment_path, :html => { :class => 'form-horizontal' } do |f| %>
Yet when i submit the form, I keep getting this error:
No route matches [PUT] "/payments/4/add_payment"
Why does it keep trying to use PUT instead of POST?
I have a form that will work sometimes and sometimes not and I can't work out what is changing between times, other than restarting the rails app. Currently it isn't working.
I have these entries in my routes file:
constraints :subdomain => 'my' do
namespace 'my', path: nil do
namespace 'author' do
resources :test_author do
resources :steps_author
[...]
end
end
end
end
The particular routes I'm interested in here produce this from rake routes
my_author_test_author_steps_author GET /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#show {:subdomain=>"my"}
PUT /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#update {:subdomain=>"my"}
DELETE /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#destroy {:subdomain=>"my"}
My form opening looks like this (using simple form and bootstrap):
<%= simple_form_for #step, :url => my_author_test_author_steps_author_path(#step), :html => { :class => 'form-horizontal' } do |f| %>
Can anyone shed some light on what is happening?
Update
Based on the help from juanpastas it seems that the form is rendering correctly however Rails is interpreting the request as a POST and not a PUT. Though for the life of me I can't work out why.
You need to pass complete params in route
<%= simple_form_for #step,
:url => my_author_test_author_steps_author_path(author, #step),
:html => { :class => 'form-horizontal' } do |f| %>
After reading your title I see you should be making POST to /author/test_author/24/steps_author/ without the last number. What about:
<% url = !#step.persisted? ? '/author/test_author/24/steps_author/' : my_author_test_author_steps_author_path(author, #step) %>
<%= simple_form_for #step,
:url => url,
:html => { :class => 'form-horizontal' } do |f| %>
I have harcoded route because I am not sure about your route helper name. You can run rake routes to find that helper.
Very new to ruby on rails and trying to get my first ajax call working. It is making the ajax call, but it always calls the #index action and seems to ignore the URL parameter. Here's the code:
class UserController < ApplicationController
def flag
logger.debug "in flag user"
respond_to do |format|
format.js { render :layout=>false }
end
end
end
In my routes.db:
resources :users do
member do
post 'flag'
get 'flag'
end
end
And then in my view I create the link like this:
<%= link_to "Flag User", :url => flag_user_path(user.id), :method => :get, :remote => true %>
the HTML source is:
Flag User
rake routes produces this:
flag_user POST /users/:id/flag(.:format) users#flag
GET /users/:id/flag(.:format) users#flag
Whenever I click on the link, the user#index method always gets executed. How do I get the user#flag method to execute?
The issue was the :url symbol. I changed:
<%= link_to "Flag User", :url => flag_user_path(user.id), :method => :get, :remote => true %>
to:
<%= link_to "Flag User", flag_user_path(user.id), :method => :get, :remote => true %>
and everything works was expected. I was incorrectly using it like link_to_remote which requires the :url symbol. Thanks everyone for their input.
The problem is in how you are defining your routes. It needs to be like this,
resources :users do
post 'flag'
get 'flag', on: :member
end
Check the rails guides for routing to get more idea.
If you run rake routes, I think you'll see that the route created with name "flag_user_path" expects "post" not "get"
I have a model 'Article' and a model 'Ratings' nested within articles.
/articles/123/ratings
I want to change the routing of the f.submit in ratings/_form.html.erb
now it is so, that after pressing submit, my application routs to
/ratings/111
but I want to route it to
/article/123
How can I change the routing in a form_for f.submit button.
I have found here something like this:
<% form_for :thing, :url =>
url_for(:action => "update", :id => #thing) do |f| %>
But this do not work for my rails 3.2. Thanks for your help,
:url - The URL the form is submitted to. It takes the same fields you pass to url_for or link_to. In particular you may pass here a named route directly as well. Defaults to the current action.
<% form_for :thing, :url => {:action => "update", :id => #thing} do |f| %>
you can also pass it the path_name by using the helper. so you can also do something like
:url => update_article_path(#article)
Try form_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}).
I have this:
<% form_tag :controller => :proposals, :method => :bulk_action do %>
but it errors in the create method... which is weird because I'm trying to tell stuff to delete.
The :method parameter specifies the HTTP request method. You're probably looking for :action instead:
<% form_tag :controller => :proposals, :action => :bulk_action do %>
This will create a form that points to bulk_action in ProposalsController.