将Devise与资源相结合:Rails中的用户

时间:2021-05-29 00:15:44

I am trying to combine Devise with a RESTful user resource using the following code in the routes.rb file:

我正在尝试使用routes.rb文件中的以下代码将Devise与RESTful用户资源结合使用:

resources :users, :only => [:index, :show]
devise_for :users

However the url localhost:3000/users/sign_up does not go to the devise sign up page, rather it produces the error "Couldn't find User with ID=sign_up", so it thinks the url is pointing to the show action of the users controller. I have found that swapping the order of the lines produces the intended behaviour:

但是url localhost:3000 / users / sign_up没有进入设计注册页面,而是产生错误“无法找到具有ID = sign_up的用户”,因此它认为该url指向显示操作的用户控制器。我发现交换行的顺序会产生预期的行为:

devise_for :users
resources :users, :only => [:index, :show]

Now when you go to localhost:3000/users/sign_up you do indeed get the sign-up page, and going to localhost:3000/users/1 hits the show action of the users controller as intended.

现在当你转到localhost:3000 / users / sign_up时,你确实得到了注册页面,然后转到localhost:3000 / users / 1按照预期命中用户控制器的show动作。

My question is this: is changing the code order like this the correct way to get devise working together with the users resource? Or is there something deeper going wrong? I suspect that merely swapping those two lines of code round can't be the solution!

我的问题是:正在改变像这样的代码顺序正确的方式来设计与用户资源一起工作?还是有更深层次的错误?我怀疑只是交换这两行代码不能解决问题!

3 个解决方案

#1


22  

My advice in these situations is to check with rake routes In routes the order in which routes are defined matters since earlier routes take precedence.

我在这些情况下的建议是检查rake路由在路由中,路由定义的顺序很重要,因为早期路由优先。

So in your case resources :users, :only => [:index, :show] created a restfull /users/:id(.:format) route that pointed to {:action=>"show", :controller=>"users"} and when you went to Devise's sign up url /users/sign-up it considered 'sign-up' an :id of the user and naturally couldnt find it.

所以在您的情况下资源:users,:only => [:index,:show]创建了一个restfull /users/:id(.:format)路由,指向{:action =>“show”,:controller =>“用户“}当你去Devise的注册网址/用户/注册时,它认为'注册'是:用户的ID并且自然无法找到它。

Now if you do the devise routing setup first, devise's routes take precedence over anything specified later and you get expected behaviour.

现在,如果您首先进行设计路由设置,则设计路径优先于稍后指定的任何路径,并且您将获得预期的行为。

#2


16  

Yes, that's the right way to do it (see https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)

是的,这是正确的方法(参见https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)

#3


2  

In the link David Sulc wrote is shown that you should write a prefix to device_for or resources Example:

在David Sulc编写的链接中显示您应该为device_for或资源编写前缀示例:

devise_for :users, :path_prefix => 'my'
resources :users

Or your users

或者您的用户

devise_for :users
scope "/admin" do
  resources :users
end

Both works well for me

两者都适合我

#1


22  

My advice in these situations is to check with rake routes In routes the order in which routes are defined matters since earlier routes take precedence.

我在这些情况下的建议是检查rake路由在路由中,路由定义的顺序很重要,因为早期路由优先。

So in your case resources :users, :only => [:index, :show] created a restfull /users/:id(.:format) route that pointed to {:action=>"show", :controller=>"users"} and when you went to Devise's sign up url /users/sign-up it considered 'sign-up' an :id of the user and naturally couldnt find it.

所以在您的情况下资源:users,:only => [:index,:show]创建了一个restfull /users/:id(.:format)路由,指向{:action =>“show”,:controller =>“用户“}当你去Devise的注册网址/用户/注册时,它认为'注册'是:用户的ID并且自然无法找到它。

Now if you do the devise routing setup first, devise's routes take precedence over anything specified later and you get expected behaviour.

现在,如果您首先进行设计路由设置,则设计路径优先于稍后指定的任何路径,并且您将获得预期的行为。

#2


16  

Yes, that's the right way to do it (see https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)

是的,这是正确的方法(参见https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)

#3


2  

In the link David Sulc wrote is shown that you should write a prefix to device_for or resources Example:

在David Sulc编写的链接中显示您应该为device_for或资源编写前缀示例:

devise_for :users, :path_prefix => 'my'
resources :users

Or your users

或者您的用户

devise_for :users
scope "/admin" do
  resources :users
end

Both works well for me

两者都适合我