2012-11-08 50 views
0

我有這個控制器。登錄後錯誤的重定向

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 

這一步defenitions黃瓜:

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

When /^he logs in$/ do 
    visit("https://stackoverflow.com/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 

一切只有經過全成登錄我得到重定向到網頁,而不是到登錄頁面succceed工作正常。所以我沒有看到閃光消息。

魯洛夫

編輯1:我檢查了控制器和RESOURCE_NAME和資源似乎有正確的價值觀。

回答

0

標準DeviseHelper爲after_sign_in_path_for是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. 

您可以檢查你的路線和範圍,甚至通過複製設計signed_in_root_path到你的ApplicationController與調試線

+0

我認爲一個愚蠢的問題,但我怎麼能檢查的範圍? –

0

默認情況下,設計的調試內部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 

個鏈接供您閱讀:

+0

你好,當我做你的建議,我看到這個錯誤:https://gist.github.com/4050770 –

+0

我解決了那一個,但現在我得到一個未初始化的常量UserController消息。這很奇怪,因爲Devise不需要用戶控制器。 –

+0

「UserController」應該是「UsersController」。聽起來像是一個錯字。 – danneu