I am including a custom method for formatting my json data.
My controller looks like this:
("def" is taken out for brevity sake)
respond_to do |format|
format.json { render json: @user, :methods => [:follower_count(params[:id])] }
end
My model looks like this:
def follower_count user_id
User.find(user_id).friendships.count
end
However, this throws a syntax error, unexpected '('.
I'm not sure how to pass in an argument to the follower_count method in the controller. I tried wrapping in square – didn't work. I tried no brackets – still doesn't work. How do you pass in arguments?!
Syntax error tells you that you're trying to use symbol :follower_count
as a function. But the main question is why are you passing user_id
to instance method when you already have it available as @id
in all instance methods of User
class.
Correct code looks something like this:
# controller
format.json { render json: @user, :methods => [:follower_count] }
# User.rb
def follower_count
friendships.count
end