1

I'm setting up a follow system with the Rails Gem acts_as_follower and I've run into a problem I'm not sure how to fix.

When I go to follow for example a user with the username of 'testuser1' I get this error:

Couldn't find Member with id=testuser1

app/controllers/follows_controller.rb:6:in `create'

Parameters:

{"_method"=>"post",
"authenticity_token"=>"FnqLCCQYcFGMerOB56/G6dlPvzpPhPDFbxCXaiDBOUU=",
"member_id"=>"testuser1"}

Here's my Controller:

class FollowsController < ApplicationController

before_filter :authenticate_member!

def create
    @member = Member.find(params[:member_id])
    current_member.follow(@member)
 end

def destroy
    @member = Member.find(params[:member_id])
    current_member.stop_following(@member)
end

end

The form to create the follow:

<%= link_to("Follow", member_follows_path(member.to_param), :method => :post, :class => "btn") %>

<%= link_to("Following", member_follow_path(member.to_param, current_member.get_follow(member).id), :method => :delete, :class => "btn btn-follow") %>

And this is how I've defined my to_param since that's how you get to a member/user's page:

def to_param
  user_name
end

Anyone out there know how I can go about fixing this? Thanks.

iamdhunt
  • 443
  • 6
  • 16

1 Answers1

0

Remove the to_param. When you're using the URL helpers, such as your member_follows_path, you need to pass the ID, or from the ERB's perspective, the object itself (it'll resolve to the ID when the ERB renders)

Alternatively, in your rails controller, change the find to something like find_by_user_name, or whatever the field is supposed to be, and then that line should work. Beware that this will be slower, especially if you have a large database without proper indexing / partitioning.

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
  • Thanks. I used the first method and it works. I have a small question about Ajax. I added :remote => true to the buttons and created the appropriate create/destroy .js files: `$("#follows_member").html("<%= escape_javascript(render :partial => "follows/follow_member", :locals => {:member => @member}) %>");` Why then isn't the button changing without a manual refresh? – iamdhunt Sep 24 '13 at 12:16
  • I thought adding: `respond_to do |format| format.html { redirect_to @member } format.js end` would make it call my create.js but it's not working – iamdhunt Sep 24 '13 at 13:03
  • @iamdhunt can you confirm for me that your browser is indeed receiving a blob of JS code (the code you specified above) and that it is properly rendered and all that? If you're in chrome, I'd use the network tab to watch the request. If it is...not sure; is the request coming back as a string instead of parsed code? – Paul Richter Sep 24 '13 at 14:17
  • No I don't see that code all I see is in the initiator column it says jquery.js?body="" – iamdhunt Sep 24 '13 at 15:15
  • @iamdhunt Where did you place the js file? In this case it should be in the same directory as your `html.erb` file(s); in this case I guess `views/followers/`, and should be named `.js.erb`. [This answer] (http://stackoverflow.com/a/3238462/877472) might also help. – Paul Richter Sep 24 '13 at 16:50
  • 1
    Nevermind I figured out what was wrong it was calling the wrong id. Thanks though. – iamdhunt Sep 25 '13 at 02:49