2012-03-21 64 views
2

我試圖找到解決方案的谷歌和這裏在SO,但無法找到...
This是唯一的問題。它只有一個答案,它是公認的,但對我不起作用......這裏是我的代碼:如何使設計RegistrationsController僅在用戶已登錄時才顯示sign_up頁面?

class RegistrationsController < Devise::RegistrationsController 

    before_filter :authenticate_user! 

    def new 
    puts "Method new was called" 
    super 
    end 

end 

當我不localhost:3000/sign_up頁面登錄正常顯示並打印Method new was called。如果我尚未登錄,我希望控制器將我重定向到sign_in頁面。當然,我可以在new方法中檢查它並重定向,但這不是一個好的解決方案...我確信有更優雅的方法。我甚至試圖使用prepend_before_filter :authenticate_user!但它不工作。

編輯

我在routs.rb

devise_for :users, :controllers => { :sessions => "sessions", :registrations => "registrations" } 
+0

你有沒有在你的設計路線中定義這個控制器類? – shingara 2012-03-21 09:09:10

+0

當然是。我會編輯我的問題 – shift66 2012-03-21 09:17:40

回答

9

Devise::RegistrationsController具有require_no_authentication before filter默認定義路線此控制器。

因此它需要跳過它:

class RegistrationsController < Devise::RegistrationsController 
    skip_before_filter :require_no_authentication 
    before_filter :authenticate_user! 

    def new 
    puts "Method new was called" 
    super 
    end 

end 
+0

謝謝,它完美的作品! – shift66 2012-03-21 12:12:00

3
skip_before_filter :require_no_authentication 

before_filter :authenticate_user! 

不起作用了。

改爲使用authenticate_scope!

相關問題