2013-11-01 127 views
0

我想使用acts_as_follower gem(link)與ajax,如here所述。我有的問題是,它遵循/取消關注,但沒有ajax(你必須按順序刷新頁面看到按鈕上的變化)acts_as_follower與ajax不起作用

這裏是我的代碼:

user.rb,create.js.erb,destroy.js.erb,_follow_user.html.erb,show.html.erb是就像第二個鏈接一樣,routes,users_controller和following_controller如下:

users_controller.rb

def show 
    @user = User.find_by_username(params[:id]) 
    @description = Description.new 
    @descriptions = Description.where(:user_id => @user.id).order("created_at desc") 
    end 

的routes.rb

resources :users, :path => '', :only => [:show] do 
resources :follows, :only => [:create, :destroy] 
end 

follows_controller.rb

def create 
    @user = User.find_by_username(params[:user_id]) 
    current_user.follow(@user) 
end 

def destroy 
    @user = User.find_by_username(params[:user_id]) 
    current_user.stop_following(@user) 
end 
+0

什麼在你嵌入_follow_user.html.erb局部的看法?該示例中的create.js.erb和destroy.js.erb假定頁面上存在一個#follow_user元素,該元素包裝_follow_user.html.erb部分。您的示例可能會有所不同或更復雜(例如,每頁有多個)。你能提供一些關於你的觀點的更多細節嗎? –

回答

1

我什麼也沒看見過最初與此所以創建了一個項目來測試。如果您已經剪切並粘貼示例中的代碼,請刪除js.erb文件中的#jQuery行,並且您可能會在新標籤頁或瀏覽器窗口中加載頁面。

這樣做後,它加載得很好,按鈕更改預期。你可以看到測試項目位置:

https://github.com/trh/social_follow_ajax

+0

非常感謝,在removng #jQuery之後完美工作!接受你的答案。 – figdig