登录后错误的重定向

时间:2012-11-08 15:29:20

标签: ruby-on-rails devise cucumber

我有这个控制器。

class SessionsController < Devise::SessionsController
  # GET /resource/sign_in
  def new
    self.resource = build_resource(nil, :unsafe => true)
    clean_up_passwords(resource)
    respond_with(resource, serialize_options(resource))
  end

  # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in, :username => resource.username) if is_navigational_format?
    sign_in(resource_name, resource)
   respond_with resource, :location => after_sign_in_path_for(resource)
 end
end

和黄瓜的这个步骤保护:

Given /^a user "(.*?)" exists$/ do |user_name|
    @user = User.create!(:username => user_name , :password => "tamara")
end

When /^he logs in$/ do 
  visit("/users/sign_in")
  fill_in('Username', :with => "roelof")
  fill_in('Password', :with => "tamara")
  click_button('Sign in')
end

Then /^he should see "(.*?)"$/ do |message|
  page.should have_content(message)
end

只有在成功登录后,一切正常,我才会重定向到主页而不是登录页面。所以我没有看到flash消息。

鲁洛夫

编辑1:我检查了控制器和resource_name,资源似乎具有正确的值。

2 个答案:

答案 0 :(得分:0)

after_sign_in_path_for的标准DeviseHelper是signed_in_root_path

  # The scope root url to be used when he's signed in. By default, it first
  # tries to find a resource_root_path, otherwise it uses the root_path.

您可以检查路由和范围,甚至通过使用调试器行将Devise signed_in_root_path复制到ApplicationController中进行调试

答案 1 :(得分:0)

默认情况下,Devise的内部after_sign_in_path_for(resource)方法首先尝试在会话中找到有效的{resource} _return_to键,然后它回退到{resource} _root_path,否则它会使用您的根路径路径。

例如,设置user_root_path会在成功登录后设置用户范围内的直接路径(可能是您想要的):

# config/routes.rb
...
devise_for :users do
 get 'users', :to => 'users#show', :as => :user_root 
end

甚至:

# config/routes.rb
...
match 'user_root' => 'users#show'

通过覆盖after_sign_in_path_for(resource)覆盖成功登录时重定向的另一种方法:

# app/controllers/application_controller.rb
...
def after_sign_in_path_for(resource)
  # Put some path, like:
  current_user_path
end

您可以阅读的链接: