我使用的是Rails 3.2,我有一个表单,我希望它通过ajax发布,并让控制器返回json.
我使用表格_作为助手,如下所示:
= form_for(@object, :remote => true, :format => :json) do |f|
....
我的对象控制器创建方法如下所示:
def create
respond_to do |format|
if @object.save
format.html { redirect_to @object }
format.json { render json: @object, status: :created, location: @object }
else
format.html { render action: "new" }
format.json { render json: @object.errors, status: :unprocessable_entity }
end
end
end
表单正在按预期提交ajaxly.但是控制器返回的是html,而不是json!
使用firebug判断请求,确保ajax请求上的内容类型http头被设置为application/html.
The documentation around this is pretty sparse, :format => :json seems to just append ".json" to the forms action, not actually modify any http headers.
I've also tried :content_type => :json to no effect.
我不能简单地硬编码控制器以返回json,因为我确实希望它在其他地方返回html...
那么,有人知道如何告诉控制器在使用form_for时呈现json吗?
谢谢你的帮助