how can I generate json from respond_to method in rails?

后端 未结 2 853
野的像风
野的像风 2020-12-28 13:55

If I have a block of code like this:

def show
  @post = Post.find(params[:id])

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


        
2条回答
  •  醉梦人生
    2020-12-28 14:20

    or you can handle it as javascript

    respond_to do |format|
      format.js { render :json { :only => :name }.to_json }
    end
    

    then you just access your action with ".js" in the end.

提交回复
热议问题