用户的不同'/'根路径,具体取决于它们是否经过身份验证(使用设计)

Fab*_*bio 16 routes ruby-on-rails devise

我正在编写一个rails应用程序,其中我有一个编辑器和一个出版物模型.我正在使用设计编辑器身份验证,并且由于编辑器不能作为访客执行任何操作,因此我编写了一个用于登录页面的自定义布局,并且我希望访客用户只能看到登录页面.

现在,我正在尝试在我的应用程序中实现以下行为,但未成功:

require 'spec_helper'
require 'capybara/rails'

describe "Authentication" do

  describe "when logged in" do
    before(:each) do
      @editor = Factory(:editor, :password => 'secret')
      visit '/'
      fill_in 'Login', :with => @editor.login
      fill_in 'Password', :with => 'secret'
      click_button 'Sign in'
      page.should have_content('Signed in successfully.')
    end

    it "getting / should render publication page with no redirection" do
      visit '/'
      page.should_not have_content('Login')
      page.should have_content('Publications')
      # assert that there is no redirection
      page.current_path.should == '/'
    end

    it "visits the sign_in page should redirect to /" do
      visit '/editors/sign_in'
      page.should have_content('Publications')
      page.current_path.should == '/'
    end

  end

  describe "when not logged in" do
    it "getting / should not display the sign in warning" do
      visit '/'
      # I want to get rid of this message
      page.should_not have_content('You need to sign in or sign up before continuing.')
    end

    it "getting / should not redirect to the sign_in default page" do
      visit '/'
      page.should have_content('Login')
      # assert that there is no redirection
      page.current_path.should == '/'
    end

    it "getting the the sign_in default path works" do
      visit '/editors/sign_in'
      page.should have_content('Login')
      page.current_path.should == '/editors/sign_in'
    end

    it "login works and redirect me to the publications page (with /)" do
      @editor = Factory(:editor, :password => 'secret')
      visit '/'
      fill_in 'Login', :with => @editor.login
      fill_in 'Password', :with => 'secret'
      click_button 'Sign in'
      page.should have_content('Signed in successfully.')
      page.current_path.should == '/'
      page.should have_content('Publications')
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

主要问题是我想摆脱"你需要在继续之前登录或注册".访客用户访问'/'时的消息.

我尝试了从这里这里采取的提示,但没有运气.

有关如何实现此设计的任何提示?

谢谢.

wil*_*key 28

我认为你应该使用类似的东西

authenticated :user do
  root :to => 'users#index', as: :authenticated_root
end
root :to => 'welcome#index'
Run Code Online (Sandbox Code Playgroud)

由于rails4不允许具有相同名称的路由,因此需要指定为:

  • 这是最正确/最新的答案 (5认同)

Fab*_*bio 24

这篇文章解释了如何实现:

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

root :to => "main#index"
Run Code Online (Sandbox Code Playgroud)

以下是执行此操作的pull请求以及一些技术细节

  • 我遵循了他的指示,但收到了@wildmonkey 暗示的关于重复路线的错误。指定 `as:` 对我来说是一个重要的缺失步骤。 (2认同)

Jos*_*que 6

authenticated :user do
  root :to => 'home#index', :as => :authenticated_root
end
root :to => redirect('/users/sign_in')
Run Code Online (Sandbox Code Playgroud)

直接来自马的嘴巴,如何为设计宝石.:d