如何覆盖 Rails 2.3 中的 RJS MIME 类型?
我有一个运行 Rails 2.3.5 的应用程序,其中大部分内容都有一个 JSON API。
一名承包商介入并在应用程序上做了一些工作,并在一些地方使用了 RJS。主网站使用 RJS 的一些控制器操作也需要成为 API 的一部分。
问题是 JSON API 请求触发 RJS 响应,这不是我想要的。我希望 RJS 响应从浏览器发生,但当它是 API 请求时(通过使用“application/json”Accept 和 Content-Type 标头来区分),那么我希望它只发送 API 响应。
据我所知,Rails 会针对任何涉及 javascript 的 MIME 类型(即
文本/javascript) 触发 RJS 响应 application/json
等。
有没有办法强制 RJS 只响应文本/javascript?或者有更好的方法来解决这个问题吗?
为了更清楚地说明,我的代码如下所示:
def show
@dashboard = @user.dashboard
respond_to do |wants|
wants.html
wants.json { render :json => @dashboard }
end
end
问题是,该控制器的视图文件夹中还有一个 show.rjs 模板。当有人点击 API 时,我希望它呈现 json 结果,正如我上面列出的那样,但它却呈现 show.rjs 模板。
如何确保 API 客户端获得我想要的 json 结果,同时仍然让 RJS 模板为网站上的用户呈现?
I have an app running Rails 2.3.5 that has a JSON API for much of it.
A contractor came in and did some work on the app and used RJS in a few places. Some of those controller actions that are using RJS for the main web site also need to be part of the API.
The problem is that JSON API requests trigger the RJS responses, which is not what I want. I'd like the RJS responses to happen from a browser, but when it's an API request (as distinguished by using the "application/json" Accept and Content-Type headers) then I'd like it to just send the API response.
From what I can tell, Rails triggers an RJS response for any MIME Type that involves javascript, i.e.
text/javascript
application/json
etc.
Is there a way to force RJS to only respond to text/javascript? Or is there a better way to solve this problem?
To make it more clear, I have code that looks like this:
def show
@dashboard = @user.dashboard
respond_to do |wants|
wants.html
wants.json { render :json => @dashboard }
end
end
The trouble is, there is also a show.rjs template in the view folder for this controller. When someone hits the API, I want it to render the json results, as I have listed above, but instead, it renders the show.rjs template.
How can I make sure that API clients get the json results I want, and still let the RJS template render for people on the website?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要在 mime_types.rb 中定义 json,然后您应该能够执行此操作:
更多内容请阅读:http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
You will need json defined in your mime_types.rb and then you should be able to do this:
More to read here: http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
在控制器的操作中尝试以下操作:
根据 Rails 文档 (http://api.rubyonrails .org/classes/ActionController/Base.html) "render :json" 将响应的内容类型设置为 application/json。
In your controller's action try the following:
According to Rails documentation (http://api.rubyonrails.org/classes/ActionController/Base.html) "render :json" sets the content type of the response to application/json.
您确定您的 JavaScript 发送了正确的标头吗?当我做 rjs 时,通常我通过响应 format.js 来做到这一点。这使我可以轻松分离 json 和 js 响应。
一般来说,我的问题是确保我的 ajax 操作实际上以正确的格式发送请求。如有疑问,您可以在请求中添加 'format':'js' 参数。在 jQuery 中:
Are you sure that your javascript is sending the correct headers? When I do rjs, generally I do this by responding to format.js. This allows my to easily separate json and js responses.
Generally, my problems have been in ensuring that my ajax actions are actually sending requests in the proper format. When in doubt, you can add a 'format':'js' parameter to your request. In jQuery: