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
It renders my job in html, however, if I request:
www.mysite.com/jobs/1.xml
I get the error:
Template is missing Missing template jobs/show.xml.erb in view path c:/workspace/mysite/app/views
What's worse is that I can also request something like
www.mysite.com/jobs/1.xyz
And indeed I see the error:
Template is missing Missing template jobs/show.xyz.erb in view path c:/workspace/mysite/app/views
To stricly present just html content, what is the cleanest and simplest way to tell Rails that I don't want to render anything other than .html.erb files.
It is important to note that:
It would be great if the solution was not at the render/responds_to level as I would have to modify a significant number of actions. Perhaps there is a way to configure Rails so that only html templates are rendered?
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.