js.erb 呈现到布局中,而不是执行

js.erb rendered into layout, not executed

本文关键字:执行 布局 erb js      更新时间:2023-09-26

我正在尝试使用简单的link_to和操作来更新跨度。 当我单击链接"什么也没发生"时,视觉上(页面不会更改)。

查看对请求的响应,我看到我的布局,其中插入了布局生成位置的 js.erb 文件的内容。

我的环境是在OS X上的Docker下运行的Rails 4.2,也是AWS上的Elastic Beanstalk。

app/views/admin/games.haml:
%p=link_to("Hint please", {:action=> "hint"}, :remote => true)
%span#hint
  where next?

app/controllers/admin_controller.rb:

  def hint
    respond_to do |format|
      format.js
    end
  end

app/views/admin/hint.js.erb:

$("#hint").text('hello coffee');

Rails 登录时单击链接:

Started GET "/admin/hint" for 192.168.99.1 at 2016-03-14 12:04:33 +0000
Cannot render console from 192.168.99.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by AdminController#hint as JS
    Rendered admin/hint.js.erb within layouts/admin (0.0ms)
    Rendered application/_favicon.haml (8.9ms)
Completed 200 OK in 197ms (Views: 196.0ms | ActiveRecord: 0.0ms)

我按照 https://www.alfajango.com/blog/rails-js-erb-remote-response-not-executing/的调试说明进行操作 - 我没有看到 Javascript 错误。响应是布局,其中 .js.erb 的内容呈现为文本,其中 yield 命令位于布局中:

<div id='main'>
    <div class='header'>
        <h1></h1>
        <h2></h2>
    </div>
    <div class='content'>
        $("#hint").text('hello coffee');
    </div>
</div>

这让我发疯,谢谢你的帮助。

也许您遭受了 Rails 错误/功能 #10768?

也就是说,如果您的主布局被称为 application.haml 而不是 application.html.haml ,就会发生这种行为。 这是因为默认情况下,没有格式扩展名的布局会为所有格式呈现。

我可以看到您正在使用 HAML,所以我猜可能是这样。