2013-03-04 57 views
0

功能我有一個部分_follow_form:阿賈克斯更新不使用Rails

<% unless current_user == @player %> 
    <div id="follow_form"> 
     <% if current_user.following?(@player) %> 
     <%= render 'unfollow' %> 
     <% else %> 
     <%= render 'follow' %> 
     <% end %> 
    </div> 
<% end %> 

我_follow部分看起來像這樣:

<%= form_for current_user.relationships.build(:followed_id => @player.id), 
      :remote => true do |f| %> 
    <div><%= f.hidden_field :followed_id %></div> 
    <div class="actions"><%= f.submit "Follow" %></div> 
<% end %> 

我create.js.erb文件:

$("#follow_form").update("<%= escape_javascript(render('players/follow')) %>") 

但是當我創建關係時沒有任何反應。我的意思是創建關係,但部分不會更新。 當我嘗試:

$("#follow_form").append("foobar"); 

它的工作原理。

我該如何更新#follow_form? 感謝您的幫助。

PS:我在發佈前搜索了一個解決方案,但是我嘗試的所有東西都失敗了。

UPDATE

有了:

$("#follow_form").html("<%= escape_javascript(render('players/unfollow')).html_safe %>")

什麼也沒有發生,部分不會被替換,但出現錯誤:

NoMethodError in Relationships#create 

Showing C:/Ruby/ostriker/app/views/players/_unfollow.html.erb where line #1 raised: 

undefined method `model_name' for NilClass:Class 

我_unfollow部分:

<%= form_for current_user.relationships.find_by_followed_id(@player), 
     :html => { :method => :delete }, 
     :remote => true do |f| %> 
<div class="actions"><%= f.submit "Unfollow" %></div> 
<% end %> 
+0

錯誤是由此行'current_user.relationships.find_by_followed_id(@player)'造成的。您是否在創建操作中聲明瞭@player? – jvnill 2013-03-04 11:32:59

+0

你說得對,我犯了一個錯誤,我宣佈@user。 – 2013-03-04 11:35:52

回答

1
$("#follow_form").html("<%= escape_javascript(render('players/follow')).html_safe %>") 
+0

感謝您的回答,但它仍然無效,並顯示錯誤。我將編輯我的帖子。 – 2013-03-04 11:25:21