开发者

How do you handle bad formats in Rails 2 routing?

How does one handle bad formats in routes in Rails 2.3? For instance suppose that you have an action that wants to handle html or json requests but nothing else how do you restrict it while allowing user-readable errors to be promulgated? The following snippet shows a start:

respond_to do |format|
  format.html # render the default
  format.json { do something appropriate }
  format.all  ?
end

The troubl开发者_JAVA百科e is what to put in place of the ?, I tried:

  format.all :text => "That's a bad format.", :status => 406

and while the status code got set appropriately the text does not get rendered (at least with a format like com, which is one that I'm receiving.

One possibility would be to change the routes file so that only the two formats were accepted, but that runs into route explosion. (I have 4 acceptable formats.) The idea of using

map.connect '/xyz.:format', :action => ..., :controller => ..., :format => '/html|json/'

sounds good but doesn't work -- it matches something like xyz.comhtml. I'm frustrated and hoping there's something I'm missing.


I might be wrong but I think for your format.all calls you can pass it a file in return.. something like this where you define the return type as well:

format.all { render :file => File.join(Rails.public_path, '406.html'), :status => 406, :content_type => 'text/html' }

And just put a "406.html" file in your public directory with that text "That's a bad format." in it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜