设计登录根路线导轨3

设计登录根路线导轨3

问题描述:

Heyya guys。 所以我想到了这个coolio的想法,如果你登录,那么你得到某种仪表板,否则你会得到一个信息/登录/注册页面..所以我该怎么做..设计登录根路线导轨3

我主要想要在路由中这样做=不是像


def index 
    if current_user.present? 
    render :action => 'logged_in' 
    else 
    render :action => 'logged_out' 
    end 
end 

在此先感谢!

/Oluf Nielsen

认为你可能一直在寻找这样的:

authenticated :user do 
    root :to => "dashboard#show" 
end 

root :to => "devise/sessions#new" 

注:这是验证* d *

+0

谢谢!正是我想要做的! – 2010-12-09 09:28:41

+1

这对我不起作用,因为我希望未登录的路由指向其他会话#new。 Shayne的答案在这些情况下是有效的,并且对我来说是解决方案......澄清:如果你设置第二个根:进入除devise/sessions之外的其他任何东西#new,它仍然会将未经身份验证的用户重定向到会话#new。 – effkay 2011-02-06 17:37:33

+5

我知道我迟到了,但如果你使用'''authenticated:user'''(注意“d”)它应该按预期工作 – 2012-01-17 22:50:50

您是否在使用devise之前的过滤器?

class FooController < ActionController::Base 
    before_filter :authenticate_user! 
... 

为什么不尝试更改默认的登录视图,以便他们有你想要的info/login/signup信息。

+0

这不是我想要的.. 我想做两个>> root:to =>“welcome#index”>根:要=>“仪表板#索引” 2010-09-25 19:34:24

这是我现在在我的应用程序布局文件中使用的。还没有把它分解成部分:

  <% if user_signed_in? %> 
       <a href="/profile"><%= current_user.email %></a> | 
       <%= link_to "Logout", destroy_user_session_path %> 
      <% else %> 
       <%= link_to "Login", new_user_session_path %> | 
       <%= link_to "Register", new_user_registration_path %> 
      <% end %> 
+0

正如我在另一个说,“这不是我想..我想做两个>>根:= = >“welcome#index”> root:to =>“dashboard#index” 2010-09-25 19:37:53

+0

In在这种情况下,请在默认根方法(指向welcome#索引的方法)中编写一个before_filter,以检查用户是否已登录,如果是,则redirect_to dashboard#index。它甚至不需要是before_filter,你可以直接在索引方法中做到这一点。 – 2010-09-26 03:23:41

+0

好吧,这就是巴洛克鲍勃卡特写的,所以如果没有其他办法让我接受他的解决方案。 Thanx的帮助:-)! – 2010-09-26 12:25:00

我也想这在我的应用程序,这是我想出了。

MyCoolioApp::Application.routes.draw do 
    root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? } 
    root :to => 'welcome#index' 

    get "/" => 'users#dashboard', :as => "user_root" 

    # .. 
end 

在Rails 3,您可以使用Request Based Contraints动态映射您root路线。上面的解决方案适用于Devise身份验证gem,但可以修改以支持您自己的实现。

随着上述root_path/将路由到未验证请求的WelcomeController#index行动。当用户登录同一root_path将路由到UsersController#dashboard

希望这会有所帮助。

+0

这正是我正在寻找的。但是,如何使用RSpec为此编写路由测试呢?在测试环境中看起来不像'authenticate?'可用?将不胜感激任何指导......谢谢。 – neezer 2011-03-14 23:16:40

+0

我不得不添加一个范围来使它工作:request.env [“warden”]。authenticate?(:scope =>:user) – hb922 2011-09-14 22:37:51

+0

此外,新的设计有一个很酷的新方法,称为authenticated,它为你做到了这一点 – hb922 2011-09-14 22:38:25

我有同样的问题,我用这个解决它:

authenticated :user do 
    root :to => "wathever#index" 
end 
unauthenticated :user do 
    devise_scope :user do 
    get "/" => "devise/sessions#new" 
    end 
end 

希望能帮助到你。

+4

这正是我所需要的。感谢发布! :) – Kirk 2012-08-22 03:36:42

+2

这工作,但不是得到“/”=> ...我的根源:“设计/会议#新”,它的工作,可能是同样的事情 - 谢谢你! – 2013-03-15 04:38:38

+1

不错的,为我工作! – 2013-08-23 14:57:34