简体   繁体   English

设计和根本路线

[英]Devise and Root Route

I am using devise (2.1.2) for the first time in a rails app. 我第一次在Rails应用中使用devise(2.1.2)。 I've followed along w/ the install procedure and generated a User model. 我遵循了w /安装过程,并生成了一个User模型。 My setup is extremely generic and I have just two controllers. 我的设置非常通用,我只有两个控制器。 I was up and running and authenticating with no problems at all for hours yesterday. 昨天我几个小时就开始运行并进行身份验证,没有任何问题。 I was logging in and out and stopping and restarting my server without issue. 我正在登录和注销,然后停止并重新启动服务器,没有问题。 Life was good. 生活是美好的。 And then out of the blue when I try to visit /main/index (protected) its redirected to users/sign_in which makes sense, but I get this error 然后当我尝试访问/ main / index(受保护)时,它突然重定向到users / sign_in,这很有意义,但是我收到此错误

No route matches {:controller=>"devise/main"} 没有路由匹配{:controller =>“ devise / main”}

Here is routes.rb 这是routes.rb

get "bid/index"
get "main/index"
get "main/feed"
get "main/admin"
get "main/fullday"
match "bid/bid/:id/:starttime/:endtime/:color" => "bid#bid"
match "bid/delete/:id" => "bid#delete"
match "main/bidfeed/:id" => "main#bidfeed"
match "main/attr/:id/:field/:value" => "main#attr"

devise_for :users
root :to => 'main#index'

I don't really understand where "devise/main" came from and I don't understand why this failed so suddenly after I restarted the server :( 我真的不明白“设计/主要”的来源,我也不明白为什么重启服务器后突然失败了:(

This usually happens when you are inside a namespaced controller (which is the case for Devise views). 这通常在您位于命名空间控制器中时发生(Devise视图就是这种情况)。

On the link_to that caused the error, try prepending a slash on the controller name. 在导致错误的link_to上,尝试在控制器名称前加一个斜杠。

Check this issue on the Devise page on Github 在Github的Devise页面上检查此问题

why that happens : under the hood, the devise_for method, on your routes.rb, actually creates a namespace for your "devise controller". 发生这种情况的原因 :在devise_for的后台, devise_for方法实际上为“ devise controller”创建了一个命名空间。 The devise view that you created belongs to that "devise controller", and thus, when you use link_to :controller => 'bla', Rails will try to find the controller bla` inside the devise namespace, which does not exist, and kaboom! 您创建的设计视图属于该“设计控制器”,因此,当您使用link_to :controller => 'bla', Rails will try to find the controller devise名称空间(不存在)和kaboom中link_to :controller => 'bla', Rails will try to find the controller bla`。 !

Ideally, you should use resource based Routes, or named routes. 理想情况下,您应该使用基于资源的路由或命名路由。 That would minimize this kind of problem, and collateraly make your code easier to read and maintain (well, this is usually the case, but this is by no means a silver bullet- use this advice with care). 这样可以最大程度地减少此类问题,并且附带条件可以使您的代码更易于阅读和维护(嗯,通常是这种情况,但这绝不是灵丹妙药,请谨慎使用此建议)。

If you are still curious, I would recommend reading the guides page about routing (very simple and straight to the chase), and the documentation of url_for (really, reaaaally helpful if you are serious about Rails). 如果您仍然感到好奇,我建议您阅读有关路由指南页面 (非常简单,直接了解内容 ),以及url_for文档 (如果您对Rails认真的话,确实有帮助)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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