Reputation: 1560
I'm trying to incorporate amistad (friendship gem) and I'm having some trouble.
Here is my controller for friendship create..
def create
invitee = User.find_by_id(params[:id])
if current_user.invite invitee
redirect_to new_friend_path, :notice => "Successfully invited friend!"
else
redirect_to new_friend_path, :notice => "Sorry! You can't invite that user!"
end
end
I have current_user defined in a session helper (with help from Michael Hartl's tutorial) as
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
The error I am getting is pointing to this line of my controller
if current_user.invite invitee
and the error states undefined method `id' for nil:NilClass
I have also tried changing the friendship controller's create action to
def create
invitee = User.find_by_id(params[:id])
if @current_user.invite invitee
redirect_to new_friend_path, :notice => "Successfully invited friend!"
else
redirect_to new_friend_path, :notice => "Sorry! You can't invite that user!"
end
end
Once I do that, it seems as though amistad's magic is forgotten, because 'invite' calls an undefined method error.
As you can tell, I'm pretty new to RoR and working on incorporating gems.
I'd appreciate any help getting this to work! Thanks, Peege
Upvotes: 0
Views: 581
Reputation: 53018
The reason you are getting that error is because, current_user
method of SessionsHelper is not getting called instead current_user
is treated as a local variable.
Add include SessionsHelper
in your controller class. So, current_user
method would be accessible to your controller.
EDIT
params[:id] is currently nil. Change the code as below:
<%= link_to 'Add Friend', friends_path(id: @user.id), :method =>"post", class: "btn btn-default btn-success" %>
Upvotes: 1
Reputation: 2256
I guess the problem is in this line:
invitee = User.find_by_id(params[:id])
post your create url, Before seeing your create request url. I can only guess: maybe there isn't id parameter, please try:
invitee = User.find(params[:user_id])
Upvotes: 1