开发者

Multiple JSON responses in a single Rails controller action

I have my show method on a controller in my application and in the response block I have

respond_to do |format|
  format.html { render :layout =>'placing', :next_days => @next_days}# show.html.erb
  format.xml  { render :xml => @artist }
  format.json { render :partial => "places/show.json", :artist => @artist } }
end

This, generates a json output based in the show.json partial in /places/1.json for example, but what if I want one more json output, like /places/alternative_1.json? 开发者_JAVA技巧How can I do that if there is already a json format block inside respond_to?

Any help is appreciated!

Cheers!


Then you will have to make some logic inside your format.json block changing what you are associating to :partial (currently is "places/show.json"), or create another route...

As format.json { render ... } is normal Ruby code, you could do something like:

format.json do 
  json_partial = json_for_this_case #example
  render :partial => json_partial, :locals => {:artist => @artist}
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜