0

我正在尝试将acts_as_follower gem(链接)与ajax一起使用,如此所述。我遇到的问题是,它跟随/取消关注,但没有 ajax(您必须刷新页面才能看到按钮上的更改)。

这是我的代码:

user.rb、create.js.erb、destroy.js.erb、_follow_user.html.erb、show.html.erb 就像在第二个链接上一样,路由、users_controller 和 follow_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

路线.rb

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

follow_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
4

1 回答 1

1

I didn't see anything initially off with this so a created a project to test. If you've cut and pasted the code from the example, remove the #jQuery lines in the js.erb files, and you might load the page in a new tab or browser window.

After doing that it loaded up fine and the button changes as expected. You can see the test project here:

https://github.com/trh/social_follow_ajax

于 2013-11-01T17:01:18.840 回答