2011-03-11 69 views
2

我希望我的Rails rest應用程序具有json作爲默認輸出格式,我也想刪除html格式。有什麼建議麼?謝謝RoR響應輸出類型

回答

2

是的,你可以做到這一點。

在你的控制器,

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

,或者你可以把它處理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 

嘗試像JSON這樣

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

如何讓默認呈現而不使用respond_to代碼塊? – Fivell 2011-03-11 12:42:18

+0

更新後的帖子。 – Ashish 2011-03-11 12:58:16