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

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

问题描述

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

resources :users, :only =>[:索引,:显示]devise_for : 用户

然而 url localhost:3000/users/sign_up 没有进入设计注册页面,而是产生错误Couldn't find User with ID=sign_up",因此它认为该 url 指向该节目用户控制器的动作.我发现交换行的顺序会产生预期的行为:

devise_for :users资源 :users, :only =>[:索引,:显示]

现在,当您转到 localhost:3000/users/sign_up 时,您确实会看到注册页面,并且转到 localhost:3000/users/1 会按预期点击用户控制器的显示操作.

我的问题是:像这样更改代码顺序是让设计与用户资源一起工作的正确方法吗?还是有更深层次的问题?我怀疑仅仅交换这两行代码并不能解决问题!

解决方案

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

所以在你的情况下 resources :users, :only =>[:index, :show] 创建了一个指向 {:action=>"show", :controller 的 restfull /users/:id(.:format) 路由=>"用户"}当您访问 Devise 的注册网址 /users/sign-up 时,它认为注册"是用户的 :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.

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

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