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 [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJun 18, '12 at 2:08p
activeJun 18, '12 at 2:08p
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Tony Jian: 1 post

People

Translate

site design / logo © 2023 Grokbase