![](/img/trans.png)
[英]acts_as_follower doesn't work fine with my Rails3.2 App
[英]Ajax doesn't work with Rails3.2
我要單擊“跟隨”按鈕,單擊后該按鈕將變為“取消關注”。 但是,當它提交表單時,它什么也不做。 為什么?
我的密碼是這樣的
-----動作(users_controller.rb)-----
def set_follow
friend = User.find_by_username(params[:username])
if f = Friendship.find(:first, :conditions => { :user_id => current_user.id, :friend_id => friend.id})
f.destroy
flash[:notice] = "Now added to follow list"
respond_to do |format|
format.html { redirect_to set_follow }
format.js
end
#redirect_to :back
else
Friendship.create(:user_id => current_user.id, :friend_id => friend.id)
flash[:error] = "Now deleted from follow list"
respond_to do |format|
format.html { redirect_to set_follow }
format.js
end
#redirect_to :back
end
end
-----表格(users / index.html.erb)-----
<div id="follow_status">
<% if user_signed_in? && current_user.friends.find_by_id(user.id) %>
<%= link_to sanitize('<i class="icon-remove icon-white"></i> ') + 'Un-Follow', follow_user_path(user.username), :class => 'btn', remote: true %>
<% elsif current_user != user %>
<%= link_to sanitize('<i class="icon-ok icon-white"></i> ') + 'Follow', follow_user_path(user.username), :class => 'btn btn-primary', remote: true %>
<% end %>
</div>
----- JS視圖(set_follow.js.erb)-----
$("#follow_status").html("<%= escape_javascript(render f) %>");
有點不同的方法,但我會用JQuery做到這一點
$('.btn').click( function() {
if( $('.btn').html() == 'Follow') {
$('.btn').html('Unfollow');
}
else if( $('.btn').html() == 'Unfollow') {
$('.btn').html('Follow');
}
});
這只是您如何做到的一個例子。 您可能想在jquery中使用更特定的類鏈
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.