ruby on rails - Unidentified Method 'id' for nil:NilClass -
i'm trying incorporate amistad (friendship gem) , i'm having trouble.
here controller 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! can't invite user!" end end
i have current_user defined in session helper (with 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 getting pointing line of controller
if current_user.invite invitee
and error states undefined method `id' nil:nilclass
i have tried changing friendship controller's create action
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! can't invite user!" end end
once that, seems though amistad's magic forgotten, because 'invite' calls undefined method error.
as can tell, i'm pretty new ror , working on incorporating gems.
i'd appreciate getting work! thanks, peege
the reason getting error because, current_user
method of sessionshelper not getting called instead current_user
treated local variable.
add include sessionshelper
in controller class. so, current_user
method accessible controller.
edit
params[:id] nil. change code below:
<%= link_to 'add friend', friends_path(id: @user.id), :method =>"post", class: "btn btn-default btn-success" %>
Comments
Post a Comment