【发布时间】:2011-08-18 18:44:15
【问题描述】:
在这种情况下,为什么 respond_with 不响应 json?我正在使用显式 .json (/tasks/4e3c1163a19d461203000106/items/4e4c27dfa19d46e0e400000a.json) 调用该操作
在我的控制器中--
class Tasks::TasksController < Tasks::BaseController
respond_to :html, :js, :json
def update
@task = @taskgroup.update_task(params[:id], params[:task])
@taskgroup.save
respond_with @task
end
end
当我覆盖 to_json 并添加断点时,它没有被命中。回应是:
{}
如果我将 respond_with 替换为对 to_json 的显式调用:
respond_with @task do |format|
format.json { render json: @task.to_json }
end
回复完美:
{
"_id":"4e4c27dfa19d46e0e400000a",
"assigned_to":null,
"comments" [{"_id":"4e4c2fd7a19d46e127000014",
[SNIP]
在后一种情况下它工作正常,但我想弄清楚为什么第一个不起作用。这发生在我的应用程序中的其他控制器和模型上。不知道它是不是一个类的东西? (rails 3.0.9 / mongoid 2.1.8)
【问题讨论】:
-
你能告诉我如果你重新订购
respond_to :json, :html, :js会继续这种行为吗? -
@Gerry 重新排序并没有帮助。
-
好吧,看看情况如何。当您在
update操作中调用respond_with时(如果对象有效),它将重定向到show操作(如果您不想要此默认行为,则必须将location: "other_action"提供给respond_with)。您能提供您的show操作吗? -
@Gerry -- 啊,对。如果存在验证错误,它可以正常工作。由于我只想返回 json 而不进行重定向,因此我将坚持使用更详细的自定义块。继续添加您的评论作为答案。
-
很高兴我在某种程度上帮助了你:)!