gpt4 book ai didi

ruby-on-rails - 用户的不同 '/' 根路径取决于他们是否经过身份验证(使用设计)

转载 作者:行者123 更新时间:2023-12-03 14:29:27 24 4
gpt4 key购买 nike

我正在编写一个 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

主要问题是我想摆脱“您需要先登录或注册才能继续”。 guest 用户访问“/”时的消息。

我尝试使用来自 here 的提示和 here但没有运气。

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

谢谢。

最佳答案

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

authenticated :user do
root :to => 'users#index', as: :authenticated_root
end
root :to => 'welcome#index'

由于 rails4 不允许与您同名的路线 需要指定为:

关于ruby-on-rails - 用户的不同 '/' 根路径取决于他们是否经过身份验证(使用设计),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5725094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com