I have a User model created in devise:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile, dependent: :destroy
after_create :build_profile
acts_as_follower
end
Which has an associated profile
with the user
. And I'm using the acts_as_follower gem to give this model the ability to follow another model.
I want the User
to follow a Profile
, so i have my profile model set up like this:
class Profile < ApplicationRecord
belongs_to :user
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "https://s-media-cache-ak0.pinimg.com/564x/f7/61/b3/f761b3ae57801975e0a605e805626279.jpg"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
acts_as_followable
end
The Profile model should be followable according to the gem, which is how I have it set up.
I ran the migrations as required rails generate acts_as_follower
In my profile_controller
iv set up the following methods:
def follow
@profile = Profile.find(params[:id])
current_user.follow(@profile)
redirect_to :back
end
def unfollow
@profile = Profile.find(params[:id])
current_user.stop_following(@profile)
redirect_to :back
end
And in my route.rb
file i have the following:
resources :profiles do
member do
get :follow
get :unfollow
end
end
Now in my profiles show view iv set up the button as so:
<% if !@profile.followed_by?(current_user) %>
<%= link_to "Follow", follow_profile_path(@profile), class: "btn btn-success btn-outline btn-sm"%>
<% end %>
According to my knowledge and documentation this should work, but for some reason it does not follow the user or create the association between the user
and the profile
to follow.
I figured it out, in my gemfile I installed from the master branch, so change your gemfile to this:
gem "acts_as_follower", github: "tcocca/acts_as_follower"