Methods for limiting the Rails render format to html

后端 未结 5 2021
没有蜡笔的小新
没有蜡笔的小新 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:00

    Ben's solution works.

    Consider the responds_to solution, though. It's cleaner since it allows flexibility when you will inevitably need to open up an action for a JavaScript json or xml call. Then you won't have to add

    skip_before_filter :allow_only_html_requests, :only => [:show]
    

    I personally like the respond_to block; it's very descriptive.

    respond_to do |wants|
      wants.html
     end
    

    Any format not specified in the block will automatically cause a HTTP 406 Not Acceptable to be returned. That's nice.

提交回复
热议问题