Tony Jian
2012-Jun-18 14:08 UTC
How to set custom type/format in controller''s response_to?
Environment ========== Rails 3.2.6 Ruby 1.9.3p194 What I found =========== class ThemesController < ApplicationController def show end end This setting will always render the /views/themes/show.html.erb page no matter what URL extension is. For example: http://localhost/themes/1.json http://localhost/themes/1.xxx http://localhost/themes/1.custom_ext ... Encounter ======== I want to run render :json=>@theme when extension is json, otherwise, render the show.html.erb page, so I changed my code: respond_to do |format| format.json { render :json => @theme} format.any {render} end It will correctly run `render :json=>@theme` when URL extension is .json, and render show.html.erb in .xml, .html, etc. However, I got 406 Not Acceptable in .xxx, .ooo, .custom_ext, and I found it is because only supported MIME type were allowed. Temporary solution ================= class ThemesController < ApplicationController def show if params[:format].present? && params[:format] == "json" render :json => @theme end end end It works fine, and when serving more then 2 formats, such as .xml, .json, .yaml, etc: class ThemesController < ApplicationController def show case params[:format] when "json" then render :json => @theme when "xml" then render :xml => @theme ... else render end end end It looks clean and not worse than respond_to style :D Question ======= 1. I am wondering if there is another better solution? 2. If case statement can do every thing respond_to can do while respond_to can''t, why should I use respond_to? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.