Reputation: 136
I'm trying to follow a tutorial to create a messaging app but while creating a conversation controller I faced an error. My goal is to check whether a conversation between the current_user and the selected user exists or not. If it doesn't exist then I want to create a new one. but when I tried this I got the error from the bellow. It would be a big help.
The error log
NoMethodError (undefined method `include?' for nil:NilClass):
app/controllers/conversations_controller.rb:30:in `conversated?'
My controller from Conversations
def create
@conversation = Conversation.get(current_user.id, params[:user_id])
add_to_conversations unless conversated?
end
private
def conversated?
session[:conversations].include?(@conversation.id)
end
Let me know if you need any other parts added to the question. Thanks in advance.
Upvotes: 0
Views: 1704
Reputation: 1636
When you are first calling session[:conversations]
, the value seems to be nil
and such include?
is not a method that nil
knows how to respond to. I think you can make a small tweak in that check like so:
def conversated?
session[:conversations]&.include?(@conversation.id)
end
The &
is a safe navigation operator where the method is only called if a value exists.
Upvotes: 1