开发者

RoR Response output type

开发者 https://www.devze.com 2023-02-16 01:03 出处:网络
I want my Rails rest application has json as defaultoutput format , also I want remove html format. Any suggestions开发者_运维问答?ThanksYes you can do it.

I want my Rails rest application has json as default output format , also I want remove html format. Any suggestions开发者_运维问答? Thanks


Yes you can do it.

In your controller,

respond_to do |format|
  #format.html # show.html.erb
  #format.xml  { render :xml => @post }
  format.json { render :json => @post }
end

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

Try like this

  format.any(:xml, :html, :json) { render :json => @post }
0

精彩评论

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