【问题标题】:Devise routes for authenticated, unauthenticated and root paths为已验证、未验证和根路径设计路由
【发布时间】:2015-11-15 05:47:42
【问题描述】:

我正在尝试根据用户是否登录来动态设置root_path。我尝试了很多变体,但是当他们使用根 URL www.mysite.com/ 时,无法将经过身份验证的用户路由到 static_pages#index .我还是 Rails 的新手,看到过类似的问题,但对我没有任何帮助(或者我没有正确实施解决方案)。

我的routes.rb:

devise_for :users

authenticated :user do
  root 'static_pages#index', as: :authenticated_root
end

unauthenticated :user do
  root 'static_pages#landing', as: :unauthenticated_root
end

当我运行rake routes 时,我会看到authenticated_root 和unauthenticated_root。当我在视图中使用这些时,我会收到如下错误:

ActionView::Template::Error (undefined local variable or method `authenticated_root' for #<#<Class:0x007ff8c37830e8>:0x007ff8c997e5b8>)

理想的解决方案: 通过身份验证定义root_path。所以,如果用户是authenticated,那么root_path 就是static_pages#index,如果不是,那么它就是static_pages#landing。我不确定这在rails 中是否可行(我还是新手)。

替代解决方案: 如果动态root_path 是不可能的,那么另一种选择是沿着authenticated_root 和unauthenticated_root 的道路前进,就像我在上面的routes.rb 中一样。

【问题讨论】:

  • 你如何在你的视图中设置调用这些?
  • 使用@satoshy的解决方案后,我现在只是使用&lt;%= link_to "Home", '/' %&gt;而不是&lt;%= link_to "Home", root_path %&gt;

标签: ruby-on-rails devise


【解决方案1】:

使用这个authenticated_root_path

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 1970-01-01
    • 2018-06-25
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    相关资源
    最近更新 更多