将Devise与资源相结合:Rails中的用户 [英] Combining Devise with resources :users in Rails

查看:105
本文介绍了将Devise与资源相结合:Rails中的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 资源中的

我正在尝试将Devise与RESTful用户资源结合使用以下代码: :users,:only => [:index,:show]
devise_for:users

但是url localhost:3000 / users / sign_up不会进入设计注册页面,而是会产生错误找不到ID = sign_up的用户,所以它认为url指向用户控制器的show操作。
我发现交换行的顺序会产生预期的行为:

  devise_for:users 
资源:users,:only => [:index,:show]

现在当你去localhost:3000 / users / sign_up你做确实得到注册页面,并且去本地主机:3000 / users / 1按预期点击用户控制器的显示操作。



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

解决方案

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



所以在你的情况下 resources:users,:only => [:index,:show] 创建一个restfull / users /:id(。:format)指向 {:action =>show,:controller =>users}
当你去Devise的注册网址 / users / sign- 它认为注册用户的:id ,并且自然找不到。



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


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

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:

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.

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!

解决方案

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.

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.

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

这篇关于将Devise与资源相结合:Rails中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆