用户的不同“/"根路径取决于他们是否经过身份验证(使用设计) [英] Different '/' root path for users depending if they are authenticated (using devise)

查看:17
本文介绍了用户的不同“/"根路径取决于他们是否经过身份验证(使用设计)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a rails app in which I have an Editor and a Publication model. I'm using devise for editors authentication and, since an editor can't do anything as guest, I wrote a custom layout to use for the login page and I want that a guest user can see only the login page.

现在我试图在我的应用中实现以下行为,但没有成功:

Now I'm trying to achieve the following behavior in my app but unsuccessfully:

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

主要问题是我想摆脱您需要先登录或注册才能继续".来宾用户访问/"时的消息.

The main issue is that I want to get rid of 'You need to sign in or sign up before continuing.' message when a guest user visit '/'.

我尝试使用来自此处的提示和这里但没有运气.

I tried with hints taken from here and here but with no luck.

有关如何通过设计实现这一点的任何提示?

Any hint on how implement this with devise?

谢谢.

推荐答案

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

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

root :to => "main#index"

这是拉取请求,其中包含一些技术细节

Here is the pull request which implement this with some technical details

这篇关于用户的不同“/"根路径取决于他们是否经过身份验证(使用设计)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆