2012-12-16 24 views
0

現在,如果我按下索引頁面上的按鈕,該按鈕會立即將'follow'更改爲'un-follow'。 但是,在展會頁面,什麼都不會發生。 我該如何解決這個問題?爲什麼Ajax不會在展會上反映,但索引?

視圖/用戶/ show.html.erb

<h1>Users#show</h1> 

<p id="notice"><%= notice %></p> 

<p> 
    <b>User:</b> 
    <%= @user.user_profile.user_id %> 
</p> 

<p> 
    <b>Language:</b> 
    <%= @user.user_profile.language_id %> 
</p> 

<p> 
    <b>Country:</b> 
    <%= @user.user_profile.country_id %> 
</p> 

<p> 
    <b>Prefecture:</b> 
    <%= @user.user_profile.prefecture_id %> 
</p> 

<p> 
    <b>Gender:</b> 
    <%= @user.user_profile.gender_id %> 
</p> 

<p> 
    <b>Nickname:</b> 
    <%= @user.user_profile.nickname %> 
</p> 

<p> 
    <b>Introduction:</b> 
    <%= @user.user_profile.introduction %> 
</p> 

<p> 
    <b>Picture url:</b> 
    <%= @user.user_profile.picture_url %> 
</p> 


<p> 
    <b>Tag List:</b> 
    <% @user.tags.each do |tag| %> 
    <span><%= link_to tag.name, {:action=>'index', :tag=>tag.name} %></span> 
<% end %> 
</p> 


<% if user_signed_in? %> 
    <div id="follow_user" data-user-id="<%= @user.id %>"> 
    <%= render :partial => "follow_user", :locals => {:user => @user} %> 
    </div> 
<% end %> 

follows_controller.rb

class FollowsController < ApplicationController 

    def create 
    @user = User.find(params[:user_id]) 
    current_user.follow(@user) 
    respond_to do |format| 
     format.js {render :action=>"create.js"} 
     end 
    end 

    def destroy 
    @user = User.find(params[:user_id]) 
    current_user.stop_following(@user) 
    respond_to do |format| 
     format.js {render :action=>"destroy.js"} 
    end 
end 

end 

視圖/用戶/ _follow_user.html.erb

<% unless user == current_user %> 
    <% if current_user.following?(user) %> 
     <%= button_to("Un-Follow", user_follow_path(user.to_param, current_user.get_follow(user).id), 
     :method => :delete, 
     :remote => true, 
     :class => 'btn') %> 
    <% else %> 
     <%= button_to("Follow", user_follows_path(user.to_param), 
     :remote => true, 
     :class => 'btn btn-primary') %> 
    <% end %> 
<% end %> 

視圖/如下/ create.js.erb

$('.follow_user[data-user-id="<%[email protected]%>"]').html('<%= escape_javascript(render :partial => "follow_user", :locals => {:user => @user}) %>'); 
#jQuery 

視圖/如下/ destroy.js.erb

$('.follow_user[data-user-id="<%[email protected]%>"]').html('<%= escape_javascript(render :partial => "follow_user", :locals => {:user => @user}) %>'); 
#jQuery 

視圖/用戶/ index.html.erb

<%- model_class = User.new.class -%> 
<div class="page-header"> 
    <h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1> 
</div> 
    <% @from %> 
    <h3>tag cloud</h3> 
    <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %> 
    <%= link_to tag.name, {:action=>'index', :tag=>tag.name}, :class => css_class%> 
    <% end %> 


<%= paginate @users %> 

<table class="table table-condensed"> 
    <thead></thead> 
    <tbody> 
    <% @users.each do |user| %> 
     <div class="memberListBox"> 
     <div class="memberList"> 
      <p class="name"><span><%= user.user_profile.nickname %></span>(<%= user.user_profile.age %>)</p> 
      <p class="size"><%= user.username %></p> 
      <p class="img"> 
      <% if user.user_profile.user_avatar? %> 
      <%= image_tag(user.user_profile.user_avatar.url(:thumb),:height => 100, :width => 100, :class => 'img-polaroid') %> 
      <% else %> 
      <%= image_tag('nophoto.gif',:height => 100, :width => 100, :class => 'img-polaroid') %> 
      <% end %> 
      </p> 
      <div class="introduction"> 
       <%= user.user_profile.introduction %> 
      </div> 

<% if user_signed_in? && current_user!=user %>   
<div class="follow_user" data-user-id="<%= user.id %>"> 
    <%= render :partial => "follow_user", :locals => {:user => user} %> 
</div> 
<% end %> 

    <%= link_to sanitize('<i class="icon-pencil icon-white"></i> ') + 'Message', new_messages_path(user.username), :class => 'btn btn-primary' %> 


       <%= link_to sanitize('<i class="icon-user icon-white"></i> ') + 'Profile', show_user_path(:username => user.username, :breadcrumb => @from), :class => 'btn btn-info' %>    

     </div> 
    </div> 
    <% end %> 
    </tbody> 
</table> 

回答

1

視圖包含與follow_userID一個DIV。

JS部分正試圖選擇follow_user

# views/users/show.html.erb 
<div id="follow_user"... 

# views/follows/create.js.erb 
$('.follow_user[data-user-id=... 

我從你的last questionfollow視圖諧音記得使用站點範圍的,所以你不能真正改變他們。因此,改變show視圖使用follow_user類:現在

# views/users/show.html.erb 
<div class="follow_user"... 
+0

啊它的作品! class和id有什麼區別?爲什麼我需要使用類而不是id? – HUSTEN

+0

類('.')標識一個或多個元素,而ID('#')恰好標識一個特定元素。元素可以同時具有類和ID,可以具有任意數量的類,但不會有多個ID。類選擇器'$('。selector')'選擇類,ID選擇器'$('#selector')選擇ID。類選擇器不能選擇ID,反之亦然。 ...如果它適合你,請不要忘記接受我的答案。 – Substantial

+0

感謝您的美麗解釋!所以當在視圖中使用@時,我們應該使用class而不是id。對? – HUSTEN

相關問題