All, I have a function within my controller that I am calling directly. However, once it executes I would like it to change to either follow or un-follow. I am using Act as Follower Gem and having some issues with changing my follow or un-follow appropriately. Can someone please point me into the right direction?
In my controller:
Createcampaigncontroller.rb
def follow
@campaign = Createcampaign.find(params[:id])
if user_signed_in? && !current_user.following?(@campaign)
current_user.follow(@campaign)
#RecommenderMailer.new_follower(@user).deliver if @user.notify_new_follower
flash[:notice] = "You are now following #{@current_user.username} #{@campaign.name}."
respond_to do |format|
format.html { redirect_to(:back) }
format.js {render :action => "follow_button" }
#redirect_to :back
end
else
flash[:error] = "You must <a href='/users/sign_in'>login</a> to follow # {@campaign.name}.".html_safe
redirect_to root_url
end
end
def unfollow
if current_user
@campaign = Createcampaign.find(params[:id])
current_user.stop_following(@campaign)
flash[:notice] = "You are no longer following #{@campaign.name}."
#format.js {render :action => "follow" }
redirect_to :back
else
flash[:error] = "You must <a href='/users/sign_in'>login</a> to unfollow #{@campaign.name}.".html_safe
end
end
In my view i was playing around with option to show button based on criteria.
<% if current_user.id == @campaignid%>
you own this
<%end%>
<%else%>
<% if current_user.following?(@campaign) %>
<%= link_to "Unfollow", unfollow_createcampaign_path(@campaign),class: "btn btn-success btn-outline btn-sm" %>
<%elsif%>
<%= link_to "follow", follow_createcampaign_path(@campaign),class: "btn btn-success btn-outline btn-sm" %>
<%end%>
This does not seem to help. I looked at some Rails example but they deal with submitting a field or form. Maybe I need to change my approach. Any help is appreciated.