2011-10-28 34 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