如何从控制器 (Rails) 获取处理类型

发布于 2024-12-13 21:18:13 字数 274 浏览 3 评论 0原文

当 HTTP 请求进入服务器时(在本例中我运行的是默认为 webrick 的rails s),消息看起来像这样:

Started POST "/cards" for 127.0.0.1 at 2011-11-05 15:04:29 -0400
  Processing by CardsController#create as JS

我想知道在这种情况下如何获取值 JS,或 HTML。抱歉,我不能更明确,我不确定该值叫什么。有人有什么想法吗?

谢谢

When an HTTP request comes into the server (in this case I'm running rails s which defaults to webrick) the message looks something like:

Started POST "/cards" for 127.0.0.1 at 2011-11-05 15:04:29 -0400
  Processing by CardsController#create as JS

I'm wondering how to get the value JS in this case, or HTML. Sorry I can't be more explicit, I'm not sure what that value is called. Anyone have any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-12-20 21:18:13

您可以使用 request.format 来获取格式。对于 JS 请求,它将返回“text/javascript”,对于 HTML 请求将返回“text/html”,对于 JSON 请求将返回“application/json”等。

另外,您通常会在 respond_to 块中使用它:

respond_to do |format|
  format.html { do something }
  format.js { do something else }
end

You can use request.format to get the format. It'll return "text/javascript" for JS requests, "text/html" for HTML, "application/json" for JSON, etc.

Also, you would typically use this in a respond_to block:

respond_to do |format|
  format.html { do something }
  format.js { do something else }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文