【问题标题】:Authenticated and unauthenticated routes for DeviseDevise 的经过验证和未经验证的路由
【发布时间】:2016-08-04 20:08:46
【问题描述】:

我使用 Rails 5 创建了一个应用程序。我的用户身份验证由 Devise gem 管理。

我需要为经过身份验证和未经身份验证的用户设置不同的根路径。我遵循了here 给出的提示。一切看起来都非常简单,但是在登录后,例如,当我点击我的“主页”链接时,我仍然被重定向到正常的 root_path。

这是我的 route.rb 代码:

authenticated :user do
  root to: 'api/v1/private/reporting/dashboards/summaries#index', as: :authenticated_root
end
root to: 'landing#index', as: :root

这是我导航栏中“主页”链接的代码:

- if api_v1_public_members_user_signed_in?
  = link_to 'Home', authenticated_root_path
- else 
  = link_to 'Home', root_path

谁能发现我可能遗漏的东西?

** 仅供参考'api_v1_public_members_user_signed_in?'方法可能看起来不熟悉,但它是必需的,因为我正在命名我的设计控制器。有关这方面的更多信息,请参阅here

【问题讨论】:

    标签: ruby-on-rails ruby devise ruby-on-rails-5


    【解决方案1】:

    尝试将经过身份验证和未经身份验证的根路径包装在 devise_scope 下,并给它们两个单独的名称:

    devise_scope :user do
      authenticated :user do
        root to: 'api/v1/private/reporting/dashboards/summaries#index', as: :authenticated_root
      end
    
      unauthenticated :user do
        root to: 'landing#index', as: :unauthenticated_root
      end
    end
    

    然后,将视图更改为:

    - if api_v1_public_members_user_signed_in?
      = link_to 'Home', authenticated_root_path
    - else 
      = link_to 'Home', unauthenticated_root_path
    

    【讨论】:

    • 似乎也没有按预期工作。我仍然被重定向到'unauthenticated_root_path'@PaulFioravanti
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多