Just to update this answer for the sake of others who happen to end up on this page.
In Rails 3, you just need to create a file at views/users/show.json.erb
. The @user
object will be available to the view (just like it would be for html.) You don't even need to_json
anymore.
To summarize, it's just
# users contoller
def show
@user = User.find( params[:id] )
respond_to do |format|
format.html
format.json
end
end
and
/* views/users/show.json.erb */
{
"name" : "<%= @user.name %>"
}