不同的'/'用户的根路径取决于他们是否经过身份验证(使用设备)
我正在编写一个 Rails 应用程序,其中有一个编辑器和一个发布模型。我正在使用 devise 进行编辑者身份验证,并且由于编辑者无法以访客身份执行任何操作,因此我编写了一个自定义布局用于登录页面,并且我希望访客用户只能看到登录页面。
现在,我试图在我的应用程序中实现以下行为,但没有成功:
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
主要问题是我想摆脱“您需要登录或注册才能继续”。当访客用户访问“/”时的消息。
关于如何使用设计实现这一点有任何提示吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
直接来自 How to设计宝石。 :D
Straight from the horse's mouth at the How to's for the devise gem. :D
另外,由于您需要在 Rails 4 中命名这些根路径,因此当您只想在说出您的徽标上使用 root_path 来分别返回仪表板或主页时,您可能会发现这很烦人。我刚刚在 ApplicationHelper 中创建了帮助程序,可以轻松解决这个问题。
Also because of you need to name these root paths in Rails 4 you might find that it is annoying when you want to just use root_path on say your logo to take you back to the dashboard or homepage respectively. I just created helpers in the ApplicationHelper that solves that easily.
是的,这个问题也让我很沮丧。
最终我只是将闪现消息隐藏在sessions#new 页面上。根本不是一个很好的解决方案,因为有时您/确实/希望显示该消息..
我在一段时间后放弃了,但我想知道您是否可以使用这个 方法,但在该 lambda 中设置一些标志,并在会话控制器中设置一个 before_filter清空闪存(如果存在)。就像......
我不太熟悉路由约束和请求对象的工作方式......但只是一个想法。仅当您尝试访问“/”而不是其他页面时,这才会关闭闪存消息。
我希望有人有一个好的解决方案!
Yeah, this issue was really frustrating for me as well.
Ultimately I ended up just hiding the flash message on the sessions#new page. Not a great solution at all since sometimes you /do/ want that message to show up..
I gave up after a while, but I wonder if you could use this approach, but set some flag inside that lambda and have a before_filter in your sessions controller that empties flash if it's present. Something like...
I'm not too familiar with how the routes constraint and the request object work.. but just an idea. This would turn off the flash message only when you're trying to access the "/" and not other pages..
I hope somebody has a nice solution!
我认为你应该使用类似的东西,
因为rails4不允许具有相同名称的路由,你需要指定为:
I think you should use something like that
since rails4 doesn't allow routes with same name you need to specify as:
这篇文章解释了如何实现这一点:
这里是拉取请求,它通过一些技术细节实现了这一点
This post explain how to achieve that:
Here is the pull request which implement this with some technical details