figdig
figdig

Reputation: 307

acts_as_follower with ajax does not work

I'm trying to use the acts_as_follower gem (link) with ajax, as described here. The problem I have is, it follows/unfollows, but without ajax (you have to refresh the page in order to see the change on the button).

Here is my code:

user.rb, create.js.erb, destroy.js.erb, _follow_user.html.erb, show.html.erb are like on the second link, routes, users_controller and follows_controller are below:

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

Upvotes: 0

Views: 212

Answers (1)

trh
trh

Reputation: 7339

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

Upvotes: 1

Related Questions