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.
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) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With