2011-11-03 55 views
18

在路徑中,我有指向"home#index"的根路徑,但是當我嘗試覆蓋與after_sign_up_path_for重新定向我登錄或註冊時根路徑。我試圖把它放在設計的子類控制器和application_controller中,但它不起作用。我需要在這裏做什麼?覆蓋設計after_sign_up_path_for不工作

應用控制器

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    def after_sign_up_path_for(resource) 
    show_cities_path(resource) 
    end 
end 

登記控制器

class RegistrationsController < ApplicationController 
    def after_sign_up_path_for(resource) 
    show_cities_path(resource) 
    end 
end 

路線

root :to => "home#index" 

回答

65

如果您還啓用了Confirmable模塊,則必須覆蓋after_inactive_sign_up_path_for,因爲新註冊處於「非活動狀態」,直到確認爲止。當Confirmable處於活動狀態時,似乎不會調用after_sign_up_path_for

+0

謝謝,這是我的問題。 – MicFin

+0

非常感謝!也有這個問題:D – marman

+0

值得注意的是,這適用於所有情況下,用戶不活躍登錄,例如,如果你重寫'active_for_authentication?'這包括可確認的情況下,用戶不能登錄,直到他們確認他們的帳戶。 –

6

我這個掙扎直到意識到我忘記宣佈我重寫了設計的註冊控制器。就我而言,我使用與設計:用戶資源,所以我說這routes.rb中:

devise_for :users, :controllers => {:registrations => "registrations"} 

在那之後,我在after_inactive_sign_up_path_for指定工作的重定向。

Override devise registrations controller有一個更完整的討論這個主題,以替代方式宣佈覆蓋。

11

儘管我遲到了比賽,但我遇到了這個問題,並且無法找到解決方案。

如果您使用自己的RegistrationsController來自定義Devise,那麼您需要將after_sign_up_path_for(resource)方法添加到該控制器而不是ApplicationController。

在registrations_controller.rb:

private 

    def after_sign_up_path_for(resource) 
    new_page_path 
    end 

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

+0

即使認爲OP問題可能已經被另一個答案更好地服務,但這是我遇到的問題! – lfzawacki

0

我剛剛吹這個約2小時,但LiveReload是我的問題。我正在成功重定向,但LiveReload正在接收development.sqllite上的更改並覆蓋請求。

0

實際上,我們可以查看devise的源代碼來解決問題,這很容易。

devise-3.4.1 $ vim app/controllers/devise/registrations_controller.rb

# POST /resource 
    def create 
    build_resource(sign_up_params) 

    resource_saved = resource.save 
    yield resource if block_given? 
    if resource_saved 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_flashing_format? 
     sign_up(resource_name, resource) 
     respond_with resource, location: after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format? 
     expire_data_after_sign_in! 
     respond_with resource, location: after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     @validatable = devise_mapping.validatable? 
     if @validatable 
     @minimum_password_length = resource_class.password_length.min 
     end 
     respond_with resource 
    end 
    end 

由於代碼顯示:

 if resource.active_for_authentication? 
     ... 
     respond_with resource, location: after_sign_up_path_for(resource) 

     else 
     ... 
     respond_with resource, location: after_inactive_sign_up_path_for(resource)