Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up acts_as_follower

I'm using the gem acts_as_follower in a rails app. I set it up and it works (In console), however I'm clueless as to how to set it up in a view. I want to make a button correspond to the user.follow and user.stop_following methods.

The github doesn't explain this. Help please.

like image 615
Vasseurth Avatar asked Dec 30 '11 19:12

Vasseurth


1 Answers

You can create controller actions that you link to. For example in an app I have the following two actions added to a user controller. Once the routes are also setup I use the url helpers to link to the actions from my view, and end up displaying the flash messages via javascript callbacks.

UsersController:

def follow
  @user = User.find(params[:id])

  if current_user
    if current_user == @user
      flash[:error] = "You cannot follow yourself."
    else
      current_user.follow(@user)
      RecommenderMailer.new_follower(@user).deliver if @user.notify_new_follower
      flash[:notice] = "You are now following #{@user.monniker}."
    end
  else
    flash[:error] = "You must <a href='/users/sign_in'>login</a> to follow #{@user.monniker}.".html_safe
  end
end

def unfollow
  @user = User.find(params[:id])

  if current_user
    current_user.stop_following(@user)
    flash[:notice] = "You are no longer following #{@user.monniker}."
  else
    flash[:error] = "You must <a href='/users/sign_in'>login</a> to unfollow #{@user.monniker}.".html_safe
  end
end

config/route.rb:

resources :users do
  member do
    get :follow
    get :unfollow
  end
end

Then in your view you can use the url helper to link to the controller action:

<%= link_to "Unfollow", unfollow_user_path(@user) %>
like image 131
JDutil Avatar answered Nov 01 '22 14:11

JDutil