简体   繁体   中英

Ruby on Rails 4: Devise with Users Controller, Couldn't find User with 'id'=sign_up

I'm getting this error: Couldn't find User with 'id'=sign_up when I'm trying to go to localhost:3000/users/sign_up in my URL browser.

I have a separate Users Controller that I generated...

I did this:

rails g devise:controllers users
rails g controller Users show

I didn't touch the controllers that was produced by devise.

In the regular users controller that I created, I have this:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

In my routes:

devise_scope :user do
  get 'users/:id' => 'users#show', as: :user
end
devise_for :users

My rake routes looks like this:

new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
                 user GET    /users/:id(.:format)           users#show

It makes sense why I'm getting an error because :id is sign_up in my URL, I just don't get what I should be doing to fix this.

And of course, it'll always give me an error for anything after localhost:3000/users/ because of the id in my show method in users controller

You have a separate users_controller which doesn't belongs to devise so devise_scope is of no use there. If you need to use any devise controller action then devise_scope is useful. So just make your routes as this:

devise_for :users
get 'users/:id' => 'users#show', as: :user

The sequence of routes matters when we have similar routes. In devise the routes sign_up , sign_out are defined as /users/sign_up(.:format) so suppose you visit sign_up url then it would check with the routes in devise and will find the match there. So it will redirect you to that route. Now if you enter /users/1 it will find in devise routes but it will find no match so ultimately it will map to your route and if there is no route defined as such it will throw error..

Glad this helped. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM