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

发布于 2024-11-02 12:59:03 字数 2470 浏览 1 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

度的依靠╰つ 2024-11-09 12:59:04
authenticated :user do
  root :to => 'home#index', :as => :authenticated_root
end
root :to => redirect('/users/sign_in')

直接来自 How to设计宝石。 :D

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

Straight from the horse's mouth at the How to's for the devise gem. :D

年华零落成诗 2024-11-09 12:59:04

另外,由于您需要在 Rails 4 中命名这些根路径,因此当您只想在说出您的徽标上使用 root_path 来分别返回仪表板或主页时,您可能会发现这很烦人。我刚刚在 ApplicationHelper 中创建了帮助程序,可以轻松解决这个问题。

module ApplicationHelper
  def root_path
    if user_signed_in?
      authenticated_root_path
    else
      unauthenticated_root_path
    end
  end

  def root_url
    if user_signed_in?
      authenticated_root_url
    else
      unauthenticated_root_url
    end
  end
end

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.

module ApplicationHelper
  def root_path
    if user_signed_in?
      authenticated_root_path
    else
      unauthenticated_root_path
    end
  end

  def root_url
    if user_signed_in?
      authenticated_root_url
    else
      unauthenticated_root_url
    end
  end
end
我是男神闪亮亮 2024-11-09 12:59:04

是的,这个问题也让我很沮丧。

最终我只是将闪现消息隐藏在sessions#new 页面上。根本不是一个很好的解决方案,因为有时您/确实/希望显示该消息..

我在一段时间后放弃了,但我想知道您是否可以使用这个 方法,但在该 lambda 中设置一些标志,并在会话控制器中设置一个 before_filter清空闪存(如果存在)。就像......

#routes
=> 'users#dashboard', :constraints => lambda {|r| r.env["skip_flash"] = true; r.env["warden"].authenticate? }


#sessions_controller.rb
before_filter :only=>[:new] do
  flash[:notice] = nil if request.env["skip_flash"]
  request.env["skip_flash"] = false
end

我不太熟悉路由约束和请求对象的工作方式......但只是一个想法。仅当您尝试访问“/”而不是其他页面时,这才会关闭闪存消息。

我希望有人有一个好的解决方案!

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...

#routes
=> 'users#dashboard', :constraints => lambda {|r| r.env["skip_flash"] = true; r.env["warden"].authenticate? }


#sessions_controller.rb
before_filter :only=>[:new] do
  flash[:notice] = nil if request.env["skip_flash"]
  request.env["skip_flash"] = false
end

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!

滿滿的愛 2024-11-09 12:59:03

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

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

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

I think you should use something like that

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

since rails4 doesn't allow routes with same name you need to specify as:

季末如歌 2024-11-09 12:59:03

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

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

root :to => "main#index"

这里是拉取请求,它通过一些技术细节实现了这一点

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文