0

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
Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62

1 Answers1

1

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

Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62
MZaragoza
  • 10,108
  • 9
  • 71
  • 116
  • still getting this error `Couldn't find Profile without an ID` and the params being sent are `{"id"=>"gurmukh-singh"}`. The id should be something like `1` or something, not sure why the names being passed because its trying to find `(params[:profile_id])` – Gurmukh Singh Jan 15 '17 at 21:49
  • It's not one because friendly Id made it into a slug – MZaragoza Jan 15 '17 at 21:55
  • its trying to find the `id` but the params its passing is the `slug`, but cant find it because there is not `id` which is stored as the `slugged` name – Gurmukh Singh Jan 16 '17 at 08:19
  • it works, but had to make a small change, iv edited the answer – Gurmukh Singh Jan 17 '17 at 19:27