简体   繁体   English

在Rails 4中禁用除HTML之外的其他格式?

[英]Disable other formats than HTML in Rails 4?

We're ending up with a lot of 我们最终得到了很多

ActionView::MissingTemplate (Missing template presentations/show, application/show with {:locale=>[:en], :formats=>["image/*"], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}

in our logs. 在我们的日志中。

The application only does HTML for the moment so I would like all other formats to return 406 (or something). 该应用程序目前仅执行HTML,因此我希望所有其他格式返回406(或其他)。 Is there any way to set this once for all render calls? 有没有办法为所有渲染调用设置一次? Or do we have to sprinkle respond_to everywhere? 或者,我们是否必须respond_to

Thanks! 谢谢!

You could add a rescue_from line to ApplicationController.rb : 您可以将rescue_from行添加到ApplicationController.rb

class ApplicationController < ActionController::Base

  rescue_from ActionView::MissingTemplate do |e|
    render nothing: true, status: 406
  end

  # ...

end

If I try to access document.xml (rather than document.pdf ) in my Rails application, Firefox shows the following message in the browser console: 如果我尝试在我的Rails应用程序中访问document.xml (而不是document.pdf ),Firefox会在浏览器控制台中显示以下消息:

GET http://localhost:3000/quotes/7/document.xml [HTTP/1.1 406 Not Acceptable  29ms]

I ended up going with a mix match of solutions since I couldn't get respond_to to work. 我最终得到了一个混合的解决方案,因为我无法respond_to工作。 This will send a 406 each time someone tries some unsupported mime type. 每次有人尝试某些不受支持的mime类型时,这将发送406。

before_filter :ensure_html

def ensure_html
  head 406 unless request.format == :html
end

在ApplicationController中放置respond_to :html

You can do it by following way 你可以通过以下方式完成

class UsersController < ApplicationController
    respond_to :html

    def index
      @users = User.all
      respond_with(@users)
    end
  end

It will respond html all actions of users controller 它将respond html users controller所有actions

Or If you want to respond_to for all controller 或者如果您想响应所有控制器的响应

then add it to ApplicationController just do 然后将它添加到ApplicationController

 class ApplicationController < ActionController
    respond_to :html
  end

and

 class UsersController < ApplicationController
    def index
      @users = User.all
      respond_with(@users)
    end
  end

If you do not use respond_with or want ActionView::MissingTemplate to be raised before the action is run (ie to prevent models being fetched etc.) you can use verify_requested_format! 如果你没有使用respond_with或者想要在运行动作之前引发ActionView::MissingTemplate (即为了防止模型被提取等),你可以使用verify_requested_format! from the responders gem. 来自响应者的宝石。

class UsersController < ApplicationController
  respond_to :html

  before_action :verify_requested_format!

  def index
    # ...
  end
end

Then verify: 然后验证:

curl -v -H "Accept: image/*" localhost:9000/users

< HTTP/1.1 406 Not Acceptable

Incidentally you can also use respond_with without passing on a model instance such as: 顺便提一下,您也可以使用respond_with而不传递模型实例,例如:

class UsersController < ApplicationController
  respond_to :html

  def index
    @users = User.all
    respond_with
  end
end

This however does not seem to be documented as far as I can see. 然而,据我所知,这似乎没有记录。

  • rails 4.2 铁轨4.2
  • responders 2.4 响应者2.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM