Rails 3 - 即使请求格式是text/html,控制器也用javascript响应
我有一个简单的场景,我想请求一个页面。请求格式为text/html。如果该请求的控制器/操作逻辑中有一些错误,我想收到一条错误消息。不同的是,我希望通过 JavaScript 响应类型(即没有页面刷新)来传达此错误消息。如果没有错误,那么我希望通过预期的 text/html 响应类型加载页面。
基本上,我想要两种不同的响应类型,具体取决于是否存在错误。这在 Rails 3 中可行吗?如果是这样,最佳实践是什么?
快速的代码示例将不胜感激。
谢谢。
I have a simple scenario where I want to request a page. The request format is text/html. If there is some error in the controller/action logic for that request, I want to get an error message. The twist is that I want this error message to be communicated to be with a javascript response type (i.e. no page refresh). If there are no errors, then I want the page to be loaded via the expected text/html response type.
Basically, I want two different response types depending on whether there is an error or not. Is this doable in rails 3? If so, what is best practice?
A quick code sample would be much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然是可行的!
我会这样做:
Sure it's doable!
I would do it like this: