根据用户的身份验证,用户使用不同的“/”根路径(使用设计) [英] Different '/' root path for users depending if they are authenticated (using devise)

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

问题描述

我正在编写一个rails应用程序,其中有一个编辑器和一个出版物模型。我正在使用device进行编辑器身份验证,因为编辑器无法做任何事情,所以我写了一个用于登录页面的自定义布局,我希望访客用户只能看到登录页面。



现在我试图在我的应用程序中实现以下行为,但失败:

  require'spec_helper'
require'capybara / rails'

描述验证do

描述登录时do
before(每个)做
@editor =工厂(:编辑器,密码=>'秘密')
访问'/'
fill_in'登录',:with => @ editor.login
fill_in'Password',:with => 'secret'
click_button'登录'
page.should have_content('登录成功')
end

它获取/应该呈现发布页面没有重定向do
visit'/'
page.should_not have_content('Login')
page.should has_content('Publications')
#断言没有重定向
page.current_path.should =='/'
end

它访问sign_in页面应该重定向到/do
visit'/ editors / sign_in'
page.should have_content('Publications')
page.current_path.should =='/'
end

end

describe当没有登录do
it得到/不应该显示登录警告do
访问/'
#我想摆脱这个消息
页面。 should_not have_content('您需要在继续之前登录或注册。)
end

它获取/不应该重定向到sign_in默认页面do
访问'/'
page.should have_content('登录')
#断言没有重定向
page.current_path.should =='/'
end

它获取sign_in默认路径工作do
visit'/ editors / sign_in'
page.should have_content('Login')
page.current_path.should =='/ editors / sign_in'
end

它登录工作并将我重定向到出版物页面(含/)do
@editor = Factory(:editor,:password => 'secret')
访问'/'
fill_in'Login',:with => @ editor.login
fill_in'Password',:with => 'secret'
click_button'登录'
page.should have_content('Signed in successfully。')
page.current_path.should =='/'
page.should have_content ('Publications')
end
end
end

主要的问题是,我想摆脱你需要登录或注册后继续消息,当一个访客用户访问'/'.



我试过其中提供了此处 here ,但没有运气。



任何关于如何使用devise实现这一点的提示?



谢谢。

解决方案

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

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

root:to => main#index

这里是拉请求,实现这一点与一些技术细节


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?

Thanks.

解决方案

This post explain how to achieve that:

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天全站免登陆