[英]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.