2012-02-22 56 views
0

在这里,我遵循Michael Hartl着名的Ruby on Rails教程(Rails 3.2)。 “11.2.5带有Ajax的工作按钮”部分适用于本地机器,但在Heroku上不起作用。 当我单击Follow或Unfollow按钮时,页面上的任何内容都不会刷新。虽然我可以看到这些行为确实是从日志中调用的。使用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 

然后我不得不手动点击浏览器的刷新按钮(火狐7.0)刷新整个页面,该按钮会在结果正确切换。

文件,应用程序/视图/布局/ 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> 

回答

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 

也许问题出在view/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 
0

我可能会迟到的广告,你可能已经发现和答案,但这个固定为我。

如果您正在使用咖啡栏宝石,则将咖啡栏宝石移到资产组之外。我把它放在jquery-rails gem之下(不要以为这很重要),但是把它放在资产组之外后,它在heroku中工作。