Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: User edit form and passwords with Devise
    text
    copied!<p>New issue I'm dealing with as I develop my first Rails (Ruby as well for that matter) application.</p> <p>I am using (in that specific order): Devise, Registration, User</p> <p>After I generated the Registration scaffolding/model, I moved the files from devise/views/registrations to /views/registrations.</p> <p>After I generated the User scaffolding, I moved edit.html.erb and show.html.erb to /views/users</p> <p>I am not using a specific admin controller, but I have some code in there that checks the current_user for admin privileges, and if so, I want to give the admin the ability to edit any user's information, without having to enter a password. So, the form I'm displaying for edit skips the password fields if the current_user is not an admin (and that part is functioning properly).</p> <p>The edit form displays fine, and the forms's fields are filled with the user being edited (not current_user) information.</p> <p>The problem I'm having is that when I hit update, I get error messages telling me that the password is missing, etc.</p> <p>I suspect that this has something to do with the edit form action (and perhaps something else).</p> <p>Here's what I have for form declaration in edit.html.erb (this is the original form declaration, when it was initially created by Devise, and it moved along with the file as I moved it to registrations and then to users view):</p> <pre><code>&lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name), :html =&gt; { :method =&gt; :put }) do |f| %&gt; </code></pre> <p>When I use firebug to see the form's HTML, this is what I see:</p> <pre><code>&lt;form id="new_user" class="new_user" method="post" action="/" accept-charset="UTF-8"&gt; </code></pre> <p>If I change registration_path to edit_user_path:</p> <pre><code>&lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name), :html =&gt; { :method =&gt; :put }) do |f| %&gt; </code></pre> <p>This is what I get in HTML:</p> <pre><code>&lt;form id="new_user" class="new_user" method="post" action="/users/user/edit" accept-charset="UTF-8"&gt; </code></pre> <p>In addition, when I try to submit the form here, it tells me:</p> <pre><code>No route matches [PUT] "/users/user/edit" </code></pre> <p>In my users_controller.rb, I have the following (I deleted the edit and update methods from the registrations controller):</p> <pre><code>def edit @user = User.find(params[:id]) end def update respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to root_url, flash[:notice] = SUCCESSFUL_REGISTRATION_UPDATE_MSG } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @user.errors, status: :unprocessable_entity } end end end </code></pre> <p>FYI: Here's what I see when I generate routes:</p> <pre><code> edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update cancel_user_registration GET /cancel(.:format) registrations#cancel user_registration POST / registrations#create new_user_registration GET /request_invite(.:format) registrations#new edit_user_registration GET /edit(.:format) registrations#edit PUT / registrations#update </code></pre> <p>Any ideas? </p> <h1>SOLUTION</h1> <p>In my case, and since I'm using update, the solution is to use update_without_password. If instead this is for new and I did not want to require a password, then it would be: save_without_password</p> <pre><code>def update @user = User.find(params[:id]) respond_to do |format| if @user.update_without_password(params[:user]) format.html { redirect_to root_url, flash[:notice] = SUCCESSFUL_REGISTRATION_UPDATE_MSG } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @user.errors, status: :unprocessable_entity } end end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload