【问题标题】:respond_to html and js if remote is true如果远程为真,则响应 html 和 js
【发布时间】:2015-06-26 15:33:37
【问题描述】:

我在控制器中有下一个代码:

def show
    respond_to { |format|
        format.html
        format.js
    }
end

我有两个视图 - show.html.hamlshow.js.haml

我在浏览器中打开了一个页面并收到了 HTML。

但是如果我使用jquery-ujs并链接remote: true,我收到了JS。

在书面文档中:

Rails 根据客户端提交的 HTTP Accept 标头确定所需的响应格式。

但是如果使用 jquery-ujs,总是接受text/javascript, application/javascript, application/ecmascript, application/x-ecmascript

在这种情况下如何接收 HTML?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    您可以将format: :html 添加到您的link-to 助手中,如下所示:

    = link_to "Product", [:admin, @product], format: :html, remote: true
    

    但是你需要一些 javascript 函数,它会处理这个 ajax 调用的回调,并对获取的 html 做一些事情

    【讨论】:

    • 恐怕是的。使用额外的format: :html 是否有问题,或者行为与您预期的不同?
    • 使用附加 format: :html 时出现问题。我写了很多代码。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多