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_countas a function. But the main question is why are you passinguser_idto instance method when you already have it available as@idin all instance methods ofUserclass.Correct code looks something like this: