2011-10-28 27 views
1

我在Ruby on Rails上使用Devise。我有幾個頁面,我正在使用一個不同的MIME類型 - 比如/myapp/products.test ...所以我已經註冊了一個叫做'test'的MIME類型,它類似於text/html ...而且我有內容協商,以顯示html.erb模板或test.erb模板...如何使設計使用不同的MIME類型/內容類型?

現在 - 當我有一個authenticate_user!方法在我的控制器 - 我正在使用'測試'MIME類型 - 我沒有正確重定向 - 我根本沒有重定向 - 我只是得到一個「你需要登錄或註冊才能繼續。 「信息。

設計中需要重寫什麼,以便在使用非HTML的MIME類型時重定向?

回答

1

我發現下面的wiki頁面上部分的答案:https://github.com/plataformatec/devise/wiki/How-To:-Make-Devise-work-with-other-formats-like-mobile,-iphone-and-ipad-(Rails-specific

所以 - 在彙總(除了登記在這個問題解釋MIME類型等):

在config /初始化/ devise.rb文件,取消對config.navigational_formats線替換爲:

config.navigational_formats = [:"*/*", "*/*", :html, :test] 

一個文件添加到初始化,並添加以下代碼行:

ActionController::Responder.class_eval do 
    alias :to_test :to_html 
end 

我還需要重寫一個設計方法,因爲我的MIME類型實際上也響應html - 但我確實想看看它是否也響應:test。所以,再一次在初始化文件夾,添加包含下列文件:

module Devise 
    module Controllers 
    # Helpers used in both FailureApp and Devise controllers. 
    module SharedHelpers 

     protected 

     # Helper used by FailureApp and Devise controllers to retrieve proper formats. 
     def request_format 
     @request_format ||= if request.format.test? 
      :test 
     elsif request.format.respond_to?(:ref) 
      request.format.ref 
     elsif MIME_REFERENCES 
      request.format 
     elsif request.format # Rails < 3.0.4 
      request.format.to_sym 
     end 
     end 

    end 
    end 
end 

這可能是因爲它本來吸塵器 - 而不是重寫上面的方法 - 在覆蓋方法監獄長,做的重定向到未經授權的網址 - 但我無法弄清楚,也不確定是否Devise(上游)不是更好的方法來覆蓋方法。

+0

關於第二個想法 - Devise SharedHelpers並非真的必要 - 您只需將您打算的respond_to:test,:html添加到您的設計控制器的頂部即可。這應該做到這一點。 – Joerg