2012-02-22 52 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中工作。