0

在这里,我正在关注 Michael Hartl 著名的 Ruby on Rails 教程(Rails 3.2)。“11.2.5 使用 Ajax 的工作跟随按钮”部分适用于本地计算机,但不适用于 Heroku。当我单击“关注”或“取消关注”按钮时,页面上的任何内容都不会刷新。虽然我可以看到该操作确实是从日志中调用的。

2012-02-22T04:05:17+00:00 app[web.1]: Started POST "/relationships" for 67.188.133.121 at 2012-02-22 04:05:17 +0000
2012-02-22T04:05:17+00:00 app[web.1]: Processing by RelationshipsController#create as JS
2012-02-22T04:05:17+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"nsYrHM0aLlEGoI19Zv8hImEmbfWPZ+gSy5xmgAV+V60=", "relationship"=>{"followed_id"=>"15"}, "commit"=>"Follow"}
2012-02-22T04:05:17+00:00 app[web.1]: Redirected to https://etl-rails32-tut-sample-app.herokuapp.com/users/15
2012-02-22T04:05:17+00:00 app[web.1]: cache: [POST /relationships] invalidate, pass
2012-02-22T04:05:17+00:00 app[web.1]: Completed 302 Found in 66ms (ActiveRecord: 61.2ms)
2012-02-22T04:05:17+00:00 heroku[router]: POST etl-rails32-tut-sample-app.herokuapp.com/relationships dyno=web.1 queue=0 wait=0ms service=75ms status=302 bytes=123

然后我必须手动单击浏览器(FireFox 7.0)的刷新按钮来刷新整个页面,并且该按钮会在结果中正确切换。

文件 app/views/layouts/application.html.erb 如下:

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= render 'layouts/stylesheets' %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <div class="container">
      <%= render 'layouts/header' %>
      <section class="round">
        <% flash.each do |key, value| %>
          <%= content_tag(:div, value, class: "flash #{key}") %>
        <% end %>
        <%= yield %>
      </section>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>
4

2 回答 2

0

我可能迟到了,您可能已经找到并回答了,但这对我来说已经解决了。

如果您使用的是 coffee-rails gem,请将 coffee-rails gem 移到资产组之外。我把它放在 jquery-rails gem 下(不认为这很重要),但是在把它放在资产组之外之后它在 heroku 中工作。

于 2013-07-11T17:25:24.973 回答
0

我刚刚完成将我的代码上传到 heroku 并且按钮工作。我也是一个n00b。我的 application.html.erb 非常接近你的。我查看了您的网站(看起来与我的非常不同),我遇到了同样的问题。我的日志显示了差异(删除了行标题):

Started POST "/relationships" for 1.1.9.1 at 2012-04-10 21:06:47 +0000
Processing by RelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jF6cuJakhooP/fIdfAhIfhy+Djgox3jukyJbm86UkVU=", "relationship"=>{"followed_id"=>"2"}, "commit"=>"Follow"}
Rendered users/_unfollow.html.erb (3.8ms)
Rendered relationships/create.js.erb (7.0ms)
Completed 200 OK in 36ms (Views: 4.2ms | ActiveRecord: 24.1ms)
cache: [POST /relationships] invalidate, pass
POST strong-stone-3754.herokuapp.com/relationships dyno=web.1 queue=0 wait=0ms service=93ms status=200 bytes=584

我的渲染,而你的重定向。

不知道这是否重要,但我从您的页面和我的页面中都抓取了 .js,并对它们进行了 wc。它们有一点不同。

18    1559  120293 mine.js
18    1343   99603 yours.js

也许问题出在视图/relationships/create.js.erb 中?

$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= @user.followers.count %>')

或者,RelationshipsContoller 的创建和销毁方法应该具有以下响应 AJAX 请求:

respond_to do |format|
  format.html { redirect_to @user }
  format.js
end
于 2012-04-10T22:43:16.540 回答