Here’s what I have in my users/edit.html/erb
<%= form_for(resource, :as => resource_name, :url => user_path(resource_name), :html => { :method => :put }) do |f| %>
My routes for user:
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
method => put, so using the routes info, I am expecting the HTML action to be /users/:id
This being users/edit.html.erb, I am expecting the HTML for from id to be edit_user_id, and class to be edit_user
Here’s what I’m actually getting in HTML (using firebug) when the form is displayed:
<form id="new_user" class="new_user" method="post" action="/users/user" accept-charset="UTF-8">
Shouldn’t the HTML have been (assuming a user id of 1):
<form id="edit_user_1" class="edit_user" method="post" action="/users/1" accept-charset="UTF-8">
In my routes.rb file, I have:
resources :users, only: [:show, :edit, :update]
devise_for :users, :controllers => { :registrations => 'registrations', :confirmations => 'confirmations' }, :path => '', :path_names => { :sign_in => "login", :sign_up => "request_invite" }
Edit – response to Viktor’s comment:
I did change the User help to:
def resource
@resource ||= User.find(params[:id])
end
So, per the API docs, this should also have picked up /users/:id as an action, so, the HTML should now look like this:
<form id="edit_user_1" class="edit_user" method="post" action="/users/1" accept-charset="UTF-8">
Instead, it is:
<form id="edit_user" class="edit_user" method="post" action="/users/user" accept-charset="UTF-8">
So, when the User controller steps into action, the following code is hit:
def update
@user = User.find(params[:id])
and it thinks that (fron the form’s action), that the id I’m passing is the word user, so I’m expecting to get the following error message when I click on update:
Couldn’t find User with id=user
and that’s exactly what I’m getting as an error. Any additional insight?
Final Answer:
In users_helper.rb:
def resource
@resource ||= User.find(params[:id])
end
and in users/edit.html.erb
resource_name, :html => { :method => :put }) do |f| %>
short answer is: it depends on the value of resource in your view.
http://apidock.com/rails/v3.2.3/ActionView/Helpers/FormHelper/apply_form_for_options!
the ids and classes for form are generated by the dom_id and dom_class helpers, the ‘action’ prefix being ‘edit’ and not ‘new’ if and only if:
for an activerecord object, this evaluates to true if and only if the object has ever been saved to db.