Search code examples
ruby-on-railsrubyruby-on-rails-4friendly-idacts-as-follower

acts_as_followers and friendly_id gems not finding id


Im using the acts_as_follower gem and friendly_id gem.

Iv set up acts_as_follower and everything is working as it should, I am able to follow Profiles as required. But now I have added the friendly_id gem to show profiles urls as profile/myname rather than profile/1.

But now the acts_as_follower gem doesn't work, it can't find the profile id to follow:

This is the set up what I'm trying now, but this still does not work.

  def follow
    @profile = Profile.find(params[:profile_id])
    current_user.follow(@profile)
    redirect_to :back
  end

  def unfollow
    @profile = Profile.find(params[:profile_id])
    current_user.stop_following(@profile)
    redirect_to :back
  end

Before it was:

@profile = Profile.find(params[:id])

The error I'm getting is:

Couldn't find Profile with 'id'=

There params that are being passed are:

{"id"=>"gurmukh-singh"} 

the id its now looking for is the friendly url name

Also the new friendly_id version requires i find profiles like this:

def set_story
  @profile = Profile.friendly.find(params[:id])
end

Solution

  • in your controller you need to change it to

      def follow
        @profile = Profile.friendly.find(params[:id])
        current_user.follow(@profile)
        redirect_to :back
      end
    
      def unfollow
        @profile = Profile.friendly.find(params[:id])
        current_user.stop_following(@profile)
        redirect_to :back
      end
    

    Then this should work