Methods for limiting the Rails render format to html

后端 未结 5 2020
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 11:18

I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so when I request a restful resource:

www.mysite.com/jobs/1

5条回答
  •  借酒劲吻你
    2020-12-09 12:13

    If you don't want to use responds_to, you can do this:

    class ApplicationController < ActionController::Base
      before_filter :allow_only_html_requests
    
      ...
    
      def allow_only_html_requests
        if params[:format] && params[:format] != "html"
          render :file => "#{RAILS_ROOT}/public/404.html"
        end
      end
    
      ...
    
    end
    

    That will run before all requests and only let those that do not specify format at all, or that specify html format through. All others get 404'd. You can create a public/406.html if you want to return 406 not acceptable.

提交回复
热议问题