2011-01-24 21 views
10

我已經看遍了所有地方,並且發現了很多信息...但是對我來說沒有任何作用,我也沒有明白:(覆蓋設計的註冊控制器,以允許在成功完成sign_up後進行重定向

我知道你在想重寫登記控制器,就像這樣:

class Users::RegistrationsController < Devise::RegistrationsController 

def after_sign_up_path_for(resource) 
    authors_waiting_path 
end 

end 

然後繼託尼Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/顯示的例子中,我應該改變我的路由更新訪問新控制器:

devise_for :users, :controllers => { :registrations => "users/registrations" } do 
#get '/author/sign_up', :to => 'devise/registrations#new' 
#get '/client/sign_up', :to => 'devise/registrations#new' 
get '/author/sign_up', :to => 'users/registrations#new' 
get '/client/sign_up', :to => 'users/registrations#new'  
end 

是的,我在這裏有點奇怪,因爲我正在抓住一些特定的路徑將它們發送到註冊頁面,這使我能夠有效地創建註冊場景。 我評論過我在重寫註冊控制器之前所擁有的。

即使這一切

和我authors_waiting_path是一個有效的路徑,它只是不斷在登記:(

這是很無奈後要到登錄頁面。

亞歷

編輯:我也在devise wiki上發現了這個:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-after-registration-(sign-up)

但是我不知道在哪裏定義這個創建方法?我應該重寫會話控制器嗎?

編輯2:

我把控制器的虛擬覆蓋:

class Pouets::RegistrationsController < Devise::RegistrationsController 

    def after_sign_up_path_for(resource) 
     authors_waiting_path 
    end 

    def new 
     super 
    end 

    def create 
     puts "was here" 
     super 
    end 

    def edit 
     super 
    end 

    def update 
     super 
    end 

    def destroy 
     super 
    end 

    def cancel 
     super 
    end 

    end 

而且我從來沒有在「在這裏」在我的日誌....我真的有一種感覺,它是完全無視越權......我必須做一些錯誤的:(

+0

您使用的是什麼版本的導軌?設計的什麼版本? –

+0

Rails 3.0.3和devise 1.1.5,如果我正確的話,兩者的最後一個版本 – Alex

回答

9

好吧...我能夠覆蓋它,就應該是:0

創建文件夾的應用程序/控制器/用戶

把registrations_controller.rb放在那裏:(選項與會話 - 但它會嘗試sign_in和以後重定向 - 它可能不是你想要的行爲)。此外,該從色器件wiki和我不知道它是否工作

class Users::RegistrationsController < Devise::RegistrationsController 

    def create 
    session["#{resource_name}_return_to"] = complete_path 
    super 
    end 

end 

重新啓動應用程序(只爲確保您不信任任何東西)


總而言之,你必須重寫創建如果您希望重定向只有用戶...如果你想定義一些更復雜的情況下,你應該猴補丁sign_in_and_redirect

讓你的控制器看起來像

class Users::RegistrationsController < Devise::RegistrationsController 
    # POST /resource/sign_up 
    def create 
    build_resource 

    if resource.save 
     set_flash_message :notice, :signed_up 

     #sign_in_and_redirect(resource_name, resource)\ 
     #this commented line is responsible for sign in and redirection 
     #change to something you want.. 
    else 
     clean_up_passwords(resource) 
     render_with_scope :new 
    end 
    end 
end 

第二選擇嘗試monkeypatch助手....

module Devise 
    module Controllers 
    # Those helpers are convenience methods added to ApplicationController. 
    module Helpers 
     def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false) 
     #intended behaviour for signups 
     end 
    end 
    end 
end 
+0

對不起,但我認爲你在混淆我想做的事情,我想重新定向avec標誌UP沒有登錄。我已經處理了登錄過程,我只是希望在激活確認電子郵件時將我的用戶引導至等待頁面。是的,你是正確的重寫sign_in路徑的方法是在主應用程序控制器中,但不幸的是註冊後的方法:)順便說一句,我認爲它應該是沒有意義的,讓這些方法遍佈各地 – Alex

+0

編輯:我添加了代碼 –

+0

呵呵我的壞,你確定你的代碼是正確地控制這個控制器嗎?也許添加一些提高「測試」來確定新的並檢查它是否會呈現正確 –

3

我已經嘗試了上述解決方案,同時它的工作原理,閱讀設計的代碼,我發現,你實際需要,以簽出剛剛註冊用戶和重定向是:

  1. 到添加is_approved或類似於您的用戶表和
  2. 添加active_for_authentication?方法在您的用戶模型

代碼:

class User < ActiveRecord::Base 

    # ... some code 

    def active_for_authentication? 
    super && is_approved 
    end 
end 

當時有點難找,當我需要它,但僅此而已。我實際上是在這裏寫的,以防別人需要它。

+0

非常有幫助,感謝提到這一點。 [文件](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Authenticatable) – Hana

相關問題