简体   繁体   English

当用户在Rails 4中使用omniauth登录时,路由到其他控制器吗?

[英]Route to a different controller when a user is signed in using omniauth in Rails 4?

How do you route to a different controller and page when a user is signed in using Omniauth (no devise) and rails 4? 当用户使用Omniauth(无条件)和rails 4登录时,如何路由到其他控制器和页面?

Current my code is: 当前我的代码是:

root to: 'dashboards#home', :constraints => lambda{|req| !req.session[:user_id].blank?}
root to: 'visitors#home'

And I get the error: 我得到错误:

"Invalid route name, already in use: 'root' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming." “无效的路由名称,已经在使用:'root'您可能已经使用:as选项定义了两个具有相同名称的路由,或者您可能会使用相同的命名覆盖已经由资源定义的路由。”

You could do it in the view: 您可以在视图中执行此操作:

<% if signed_in? %>
   <%= render 'dashboards/home' %>
<% else %>
   ...whatever the non-signed in person sees
<% end %>

Then you only have one root that can lead to two different pages whether the user is logged in or not. 这样,无论用户是否登录,您只有一个根可以导致两个不同的页面。

root to: 'vistors#home'
match '/dashboard',   to: 'dashboards#home', via: 'get

I usually just do this in the controller. 我通常只是在控制器中执行此操作。 For example, root all to dashboard: 例如,将所有都植根于仪表板:

root to: 'dashboards#home'

Then in the DashboardController: 然后在DashboardController中:

def home
  if session[:user_id].blank?
    redirect_to controller: 'visitors', action: 'home'
  end
end

I haven't found a graceful way to do conditional routing in rails, unfortunately. 不幸的是,我还没有找到在Rails中进行条件路由的优雅方法。

暂无
暂无

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

相关问题 在Rails中使用Omniauth时,如果该用户存在并使用其他身份验证,则Google_omniauth2会创建一个新用户 - When using Omniauth in Rails, Google_omniauth2 creates a new user if the user exists and use different auth 如何将使用omniauth(Rails with Devise)注册的用户重定向到特定页面? - How to redirect a user signed up with omniauth (Rails with Devise) to a specific page? 使用 omniauth 和 Facebook 对 Rails API 的用户进行身份验证? - Authenticate user using omniauth and Facebook for a rails API? 使用Rails中的Omniauth重定向登录的用户 - Redirect logged in user using Omniauth in Rails 用户登录后是否可以呈现其他页面? Rails 4设计 - Is it possible to render a different page when user is signed in? Rails 4 devise 使用Omniauth(Rails)时,Twitter uid为null - Twitter uid is null when using Omniauth (Rails) Rails:检查用户是否从另一个控制器登录 - Rails: Checking if a user is signed in from another controller 单击指向root_path的链接时,没有路由与{:action =&gt;“ show”,:controller =&gt;“ users”}匹配-用户已登录 - No route matches {:action=>“show”, :controller=>“users”} when clicking the link to root_path - user is signed in 使用Omniauth for Rails运行测试时无法设置current_user - Using Omniauth for Rails I can't get current_user set when running my test 使用Rails Omniauth gem和Google OpenID时,如何不要求用户发送电子邮件 - How do I NOT require user's email when using Rails Omniauth gem and Google OpenID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM