Rails 3.2 .js.erb呈现但从未触发

时间:2012-02-03 04:30:21

标签: javascript ruby-on-rails ruby-on-rails-3.2

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

# 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)

即使日志显示.js.erb,“alert()”也不会触发,也不会放入Rendered petitions/create.js.erb (0.0ms)视图中的任何其他内容。我之前已经在其他(非3.2)项目中完成了这项工作,所以我确信我正在做一些明显愚蠢的事情,或者这里发生了一些非常偷偷摸摸的事情。


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

1 个答案:

答案 0 :(得分:0)

您应该在PetitionsController

的顶部添加以下行
respond_to :js, :only => :create