2013-05-27 68 views
1

我讀過&嘗試張貼在衆多的SO職位的解決方案(即herehere,並且here)以及制定的回答上how to change the path after a failed registration,並制定的RegistrationsController code,都無濟於事。重定向到root_path失敗後製定的用戶註冊

而不是做在/lib/文件夾中的自定義失敗的方法最喜歡的建議的,這似乎是解決最容易的地方/覆蓋,這將是在RegistrationsController#create方法在它的底部:

else 
    clean_up_passwords resource 
    respond_with resource 
end 

這是(我假設)正確地迴應了user(即將它們重定向到root_path/users),但這裏有一個棘手的部分:我在用戶註冊時創建了一個嵌套模型,這對於Devise來說非常困難。我擔心,如果我搞砸RegistrationsController#create方法,我會打破我的完美工作嵌套模型。

有人還提到Devise解決方案沒有爲他們工作,但後來得到它改變路由問題後工作。我懷疑是這種情況我,但這裏是我的routes.rb文件,以防萬一:

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

    resources :users do 
    resources :lockers 
    end 

    resources :lockers do 
    resources :products 
    end 

    resources :products 
    resources :lockups 

    match '/user/:id', :to => 'users#show', :as => :user 

    root :to => 'home#index' 

我完全明白任何幫助,任何人都可以提供給我。我對Rails仍然很陌​​生,但我總是樂於學習。

編輯:感謝激情,我一直在試圖找出如何改變註冊控制器中最後else塊的邏輯。下面就是我(以及我的小白邏輯)嘗試:

# Obviously cleared all the fields since it made no mention of resource 
    # redirect_to root_path 

    # Too many arguments since root_path takes 0 
    # redirect_to root_path resource 

    # Just bombed, something about a String somethingorother 
    # render root_path 

    # Heh, apparently call 'respond_with' and 'redirect_to' multiple times in one action 
    # respond_with resource 
    # redirect_to root_path 
+0

你可以請嘗試更詳細。爲什麼不重寫'devise/registrations'的'else'塊。你面臨什麼問題? –

+0

@激情我可以嘗試,但是我嘗試重寫'RegistrationController#create'方法將近十個小時,我每次都設法搞砸它。我不理解Ruby的繼承/重寫,甚至不知道如何嘗試,而不必擔心已經破壞了我的功能註冊。 –

回答

0

首先,你必須修復routes.rb文件。由於您使用自定義控制器,因此您還必須自定義devise_scope塊。變化

get "signup", :to => "devise/registrations#new" 

get "signup", :to => "registrations#new" 

而且,如果你試圖覆蓋的方法,並要設置的root_path色器件軌註冊後,就可以像

# app/controllers/registrations_controller.rb 
class RegistrationsController < Devise::RegistrationsController 
    def create 
    build_resource 
    // The `build_resource` will build the users . 
    if resource.save 
    if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_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_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
    end 
    else 

    clean_up_passwords resource 
    ## replace your logic here 
    redirect_to root_path 
    end 
    end 
end 

請注意,上述代碼適用於devise 2.2.4。目前,由於rails 4和strong_parameter的兼容性,github上master分支上的代碼有所改變。

+0

很高興看到我走在正確的軌道上,因爲你提到的那部分'##替換你的邏輯'是我花了很長時間試圖弄清楚的。我的註冊表單位於我的主頁上,我只是想將它們重定向回註冊失敗的主頁。我已經添加了所有我試過的問題。 –

+0

@KyleCarlson,請查看http://shrouded-meadow-3520.herokuapp.com/我特意爲您創作,可能會對您有所幫助。 –