Rails action_controller 控制器处理 ( #show as */* )
我正在向我的一个控制器#action发送一个ajax请求,但我的开发日志显示,
Processing by FormsController#show as */*
这应该是FormsController#show as JS,因为它是一个ajax请求,
因此它渲染了它在我的响应块中第一个找到的格式在控制器中
例如:-
respond_to do |format|
format.html{ }
format.js { }
end
如果我向控制器发送 ajax 请求,它不会按预期工作,因为我的控制器呈现 html 响应,而它应该处理 JS 请求。
但如果 respond_to 块是这样的,
respond_to do |format|
format.js { }
format.html{ }
end
它就会按预期工作。
我认为
Processing by FormsController#show as */*
是响应它在respond_to 块中首先找到的任何格式。
但我担心的是为什么显示的是我的开发日志,
Processing by FormsController#show as */*
而不是
Processing by FormsController#show as JS
我发送 ajax 请求的时间。我是否做错了什么或遗漏了一个小而重要的部分?
I am sending an ajax request to one of my controller#action but my development log shows
Processing by FormsController#show as */*
while this should have been FormsController#show as JS as its an ajax request
Due to this the its rendering the format that it finds first in my responds to block in the controller
Eg:-
respond_to do |format|
format.html{ }
format.js { }
end
if I send ajax request to my controller it doesn't work as expected as my controller renders html response while it should process JS request.
But if the respond_to block is in this way
respond_to do |format|
format.js { }
format.html{ }
end
It work as expected.
I think
Processing by FormsController#show as */*
is responding to which ever format it finds first in respond_to block.
But my concern is why my development log is showing
Processing by FormsController#show as */*
instead of
Processing by FormsController#show as JS
when I have sent an ajax request. Am I doing something wrong or missing a small but important piece?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新您的 Rails.js 文件。
我假设您正在使用 jQuery-Rails。不久前,Rails AJAX 请求不会自动设置请求的内容类型。此问题已得到解决,因此如果您进行更新,则不应将
*/*
作为 HTTP 标头中最高优先级的内容类型。Update your Rails.js file.
I'm assuming you're using jQuery-Rails. Some time ago, Rails AJAX requests didn't automatically set the requested content-type. That has since been addressed, so if you update you shouldn't have
*/*
as the highest-prioritity content-type in your HTTP headers.