SeattleDucati
SeattleDucati

Reputation: 293

Rails -Couldn't find User with 'id'=

I've been developing a site based on the Hartl's guide. I want to move the password reset functionality to it own partial.

I created a partial, _passreset.html.erb. Essentially a copy of the _form that only shows your username and displays the password/password_confirm fields.

But I'm not even getting there. On the show.html.erb page (the profile page), I have links to Edit and Password Reset. Here is the code:

   <% if current_user.admin? || current_user == @user %>
   <%= link_to "Edit", edit_user_path(@user) %> | 
   <%= link_to "Password Reset", user_passreset_path(@user)%>
   <% end %>

When I click on the "Password Reset" link, I get the follow error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with 'id'=passreset

The source is my users controller set_user function:

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

The route for the new function looks like this:

resources :users do
resources :articles
get 'passreset'
end

rake routes shows this line:

 user_passreset GET    /users/:user_id/passreset(.:format)                 users#passreset

I've also explicitly declared the route (get 'users/passreset'), it doesn't make a difference.

Why is it looking for an id of 'passreset'? I don't get it.

I don't think it matters, but this is the passrest function from my users controller:

 def passreset
 respond_to do |format|
  if @user.update(user_params)
    format.html { redirect_to @user, notice: 'Password was      successfully updated.' }
    format.json { render :show, status: :ok, location: @user }
  else
    format.html { render :edit }
    format.json { render json: @user.errors, status:  :unprocessable_entity }
  end
end
end

Upvotes: 0

Views: 1040

Answers (1)

Doon
Doon

Reputation: 20232

if you look at your route output , it is looking for a user_id as part of the nested route

user_passreset GET    /users/:user_id/passreset(.:format)  

if you make the route on the member, it should match based on :id again.

resources :users do
  resources :articles
  member do 
    get 'passreset'
  end
end 

Upvotes: 2

Related Questions