Houdini
Houdini

Reputation: 3542

Rails - updating a user returns `Couldn't find User with id=edit`

In views/users/index.html.erb I have:

<% @users.each do |user| %>
  <% if user.teacher == current_user.teacher || current_user.role == "admin" %>
  <tr>
    <td><%= user.username %></td>
    <td><%= user.email %></td>
    <td><%= user.teacher %></td>
    <td><%= user.role %></td>
    <td><%= user.admin %></td>
    <td><%= link_to 'Problems', student_problems_path(user_email: user.email) %></td>
    <td><%= link_to 'Edit', edit_user_registration_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
  <% end %>
<% end %>
</table>

When you click on Edit, you are linked to views/users/edit.html.erb, which looks like this:

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, :as => resource_name, :url => user_registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :teacher %><br />
  <%= f.text_field :teacher, :autofocus => true %></div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div><%= f.submit "Update" %></div>
<% end %>

The problem is, when I click on Update and actually try to update the model, I get

Couldn't find User with id=edit
in app/controllers/users_controller.rb:51:in `update`

Here are the update and edit actions in the UsersController:

def update
    @user= User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
end

def edit
    @user = User.find(params[:id])

    redirect_to(user_path)
end

And, if it helps, here are my routes:

                  root        /                              problems#new
        feedback_index POST   /feedback(.:format)            feedback#create
          new_feedback GET    /feedback/new(.:format)        feedback#new
      new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
          user_session POST   /users/sign_in(.:format)       devise/sessions#create
  destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
         user_password POST   /users/password(.:format)      devise/passwords#create
     new_user_password GET    /users/password/new(.:format)  devise/passwords#new
    edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                       PUT    /users/password(.:format)      devise/passwords#update
edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
     user_registration PUT    /users(.:format)               devise/registrations#update
                 users GET    /users(.:format)               users#index
                       POST   /users(.:format)               users#create
              new_user GET    /users/new(.:format)           users#new
             edit_user GET    /users/:id/edit(.:format)      users#edit
                  user GET    /users/:id(.:format)           users#show
                       PUT    /users/:id(.:format)           users#update
                       DELETE /users/:id(.:format)           users#destroy
              problems GET    /problems(.:format)            problems#index
                       POST   /problems(.:format)            problems#create
           new_problem GET    /problems/new(.:format)        problems#new
          edit_problem GET    /problems/:id/edit(.:format)   problems#edit
               problem GET    /problems/:id(.:format)        problems#show
                       PUT    /problems/:id(.:format)        problems#update
                       DELETE /problems/:id(.:format)        problems#destroy
            help_about GET    /help/about(.:format)          help#about
          problems_new GET    /problems/new(.:format)        problems#new
                  data        /data(.:format)                problems#data
      student_problems        /student_problems(.:format)    users#student_problems

Thanks for anyone that looks at this problem for me, it has been coming up a lot lately.

Upvotes: 0

Views: 1182

Answers (4)

Timoun
Timoun

Reputation: 49

In routes.rb you may have put this two lines in the wrong order :

#routes.rb resources :users devise_for :users

then calling mysite/users/edit will be routed to user controller with id 'edit'

it should be : #routes.rb devise_for :users resources :users

Upvotes: 0

sumit joshi
sumit joshi

Reputation: 95

Yes Nick is right. for editing user's profile there is no need to user edit_user_registration_path(user). This path is probably used when you create new user with the help of Devise.

Here you have to make sure you pass an id with your url to update user. It would be something like this

edit_user_path(@user.id)

or

{:controller=> 'user', :action => 'edit', :id => @user.id}

Upvotes: 0

boulder
boulder

Reputation: 3266

Nick is correct that you need to use edit_user_path, which routes you to Users#edit, instead of to the Devise registrations controller.

The error message you're getting indicates that the required id parameter in the edit_user_path helper is missing, which very likely means that one of your users in @users is not saved (its id is nil). To debug try something in your view like:

<% @users.each do |user| %>
   USER ID = <%= user.id %>
<% end %>

You'll probably find that one of the values is blank.

Upvotes: 0

n_i_c_k
n_i_c_k

Reputation: 1534

You're calling the wrong path. In your view change

edit_user_registration_path(user)

to

edit_user_path(user)

Upvotes: 1

Related Questions