Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails respond_to redirect all requests except X mime-type

Below is the typical respond_to block im using in my controller

  respond_to do |format|
    format.html # show.html.erb
  end

I want to restrict all mime-types except html(lets say). Couldn't come up with a solution, how is this possible? This block does nothing if the request is json, this is OK but what I want is to redirect any requests that are not html.

Thanks

like image 355
dmtzz Avatar asked Jan 18 '13 07:01

dmtzz


1 Answers

The format object yielded by respond_to has all of the usual mime types (html, js, xml, etc), and it also has a catch-all mime type any that will handle everything else. So, in this case:

respond_to do |format|
  format.html
  format.any { redirect_to :foo }
end

will use default rendering for html, and will redirect for everything else. See the docs for (a tiny bit) more information: http://apidock.com/rails/ActionController/MimeResponds/respond_to

like image 100
Jim Deville Avatar answered Nov 04 '22 10:11

Jim Deville