gpt4 book ai didi

javascript - Rails 3.2 .js.erb 呈现但从不触发

转载 作者:行者123 更新时间:2023-11-30 06:43:18 26 4
gpt4 key购买 nike

我遇到了一个非常令人困惑的问题。我以前做过,但无法弄清楚为什么它在这种情况下不起作用,如果有人知道从哪里开始寻找,我将不胜感激。

# app/views/petitions/new
<%= form_for @petition, :remote => true do |form| %>
<%= form.submit "Submit", :class => "primary" %>
<% end %>


# app/controllers/petitions_controller
def create
# I've stripped everything out of here to isolate the issue
end

# app/views/petitions/create.js.erb
alert("Hello World");

# log
Started POST "/petitions" for 127.0.0.1 at 2012-02-02 23:07:43 -0500
Processing by PetitionsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bWXmagSz0hVqhQ1/1U480kQUYcIhmSEbNMAofWYT2Oc=", "petition"=>{"name"=>"", "email"=>"", "location"=>""}, "commit"=>"Submit"}
Rendered petitions/create.js.erb (0.0ms)
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
SQL (0.6ms) UPDATE `users` SET `last_activity_at` = '2012-02-03 04:07:43' WHERE `users`.`id` = 1
Completed 200 OK in 5ms (Views: 2.5ms | ActiveRecord: 0.9ms)

“alert()”永远不会触发,我放入 .js.erb View 中的任何其他内容也不会触发,即使日志显示 Rendered petitions/create.js.erb ( 0.0 毫秒)。我以前在其他(非 3.2)项目中这样做过,所以我确信我正在做一些非常愚蠢的事情或者这里发生了一些非常偷偷摸摸的事情。


更新 - Firebug 似乎在告诉我,我收到的内容类型是“text/html”而不是“text/javascript”。我仍然不确定为什么会发生这种情况或从哪里开始寻找原因,如有任何提示,我们将不胜感激。

最佳答案

您的 PetitionsController 顶部应该有以下行:

respond_to :js, :only => :create

关于javascript - Rails 3.2 .js.erb 呈现但从不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9123703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com