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.
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.
Related
hey guys so i'm having issues adding a Like/Dislike function to my bookmarks on my page.
basically i have a snippet of code given to me that lives in my User model:
def liked(bookmark)
likes.where(bookmark_id: bookmark.id).first
end
however when i am running the server and clicking on the topic to show the associated bookmarks, i keep getting the
undefined method `id' for nil:NilClass
my question is... firstly what is going wrong here? and secondly, whats the difference between bookmark_id and bookmark.id?
im pretty sure id doesn't exist for bookmark... and if not... how would i add it?
ive tried via migration, unfortunately nothing great came from that
use this code:
def liked(bookmark)
likes.where(bookmark_id: bookmark.id).first if bookmark.present?
end
You are getting id for nil:NilClass error due to object is not present.i.e bookmark object is nil.
bookmark_id is the field name for the bookmark class.And bookmark.id returns the id of the bookmark object, only if the object is present.
bookmark.id raises an exception.
Ensure bookmark instance is passed to the liked method and is not nil
You need to ensure that your bookmark argument is not nil.
You could try the following code
def liked(bookmark)
likes.where(bookmark_id: bookmark.try(:id)).first
end
or an even better version
def liked(bookmark)
likes.find_by(bookmark_id: bookmark.try(:id))
end
Above code will return a nil object or first like
To answer your second question, bookmark_id is an column name here for your Like model whereas bookmark.id is a method call on bookmark object.
You can try the following.
def liked(bookmark)
likes.where(bookmark_id: bookmark.id).try(:first) unless bookmark.blank?
end
With the koala gem I am trying to count checkins for a page. I am using rails.
In my user.rb I have a method for getting a new connection to the Facebook graph:
class User < ActiveRecord::Base
def facebook
#facebook ||= Koala::Facebook::API.new(oauth_token)
end
end
In my school.rb I have a method for counting the checkins:
class school < ActiveRecord::Base
def count_checkins(name)
checkins = #facebook.fql_query("SELECT checkins FROM page WHERE name = #{name}")
end
end
And I am calling it from the view like this:
<%= #school.count_checkins(#school.name) %>
But I get the following error:
undefined method `fql_query' for nil:NilClass
Dont really understand why I get this error, any help would be wonderful.
It looks like you haven't actually created the #facebook object inside your School model. We'd need to see the rest of your school.rb file to know for sure. I'd suggest you create the object inside your School.initialize() method like so:
def initialize(oauth_token)
unless oauth_token.nil?
#facebook = Koala::facebook::API.new(oauth_token)
end
end
In order for this to work, you'll need to pass the desired oauth_token to your School.new() call. Then you'll have one #facebook object for each School.
Edit
After looking at the gist, I realized that you had actually intantiated a User object, and called the facebook method on that. That is actually the better way to do it. The problem is, you're using #current_user, which would have to be setup as a property of the school model. You probably meant to use the helper function current_user instead.
def count_checkins(name)
u = current_user
u.#facebook.fql_query("SELECT checkins FROM page WHERE name = #{name}")
end
Try that and see what happens. At the very least, you should get a different error message.
Edit 2
So, now I'm thinking the current_user function should be called in controller code, not model code. This is because the current user is something that doesn't really exist except as part of an active request. Therefore, we should take User u as a parameter to the count_checkins function like so:
def count_checkins(name, u)
u.facebook.fql_query("SELECT checkins FROM page WHERE name = #{name}")
end
You'll need to change the code where you call count_checkins() too:
count_checkins(name, current_user)
That should do it. Let's see!
I'm getting this error "undefined method `attribute_method_matcher' for nil:NilClass".
My controller name is Cad Its function is
def index
#cadempty = Cad.new
#caddata = Cad.all
end
The error is on creating the new object. If I comment Cad.new the code works fine.
Earlier I thought it could be because I have a method named 'new' and I was Using User.new to create a blank object for the form. But its not the error I renamed the method to something else and the error still exists. I have no idea what I'm doing wrong.
Maybe one of your column names in the database table is a reserved word.
Avoid using names for methods that are reserved words in the language.
I have two models, User and Profile, in one-to-one relation and I am trying to create a new profile for a user if it does not exist yet:
user = User.includes(:profile).find( params[:user_id] )
unless user.profile.present?
user.profile.create
end
But I am getting an error: undefined method `create' for nil:NilClass
Well, two things. Firstly, I assume the code is wrong, as that only enters the block if the profile is there (and hence can't create it).
if user.profile.blank?
user.profile.create
end
looks like more correct code.
Secondly, when you use a has_one, you don't use .create like you do with has_many. This is because the relation object is directly returned, and not a "proxy" method like a has_many. The equivalent method is create_profile (or create_x where x is the object)
Hence, try the following code:
if user.profile.blank?
user.create_profile
end
My validations were working for a while, or so I thought. Now I come back to them after a while doing something else and I am getting the error above. I think it means I am creating a nil object with the .new method but I am just lost. It seemed to be a problem with the creation of a new object by the controller because even if I # out the validation the next validation in the tree on another attribute of #thing throws up the same error. However even if I create the object in the console and test it's there the .save method throws up the error still - undefined method 'user_id' for nil:NilClass
ThingsController:
def create
#thing = Thing.new(params[:thing])
#thing.user_id = #currentuser_id
if #thing.save
flash[:notice] = "Successfully created thing."
redirect_to #thing
else
#flash[:notice] = "Your thing did not get created."
render 'otherthings/show'
end
end
Thing.rb
validate :user_valid
def user_valid
errors.add("you must be logged in to add a thing") unless #thing.user_id?
end
I'm a bit of a ruby on rails noob (i.e. <8weeks) and this is my first stackoverflow question, so go easy on me if this is stupidly obvious. I've tried params["thing"] too, as that worked in the console after manually creating params to match the log files (whereas params [:thing] didn't) but that doesn't change anything in terms of the error message I get.
When you are calling unless #thing.user_id?, it's flipping out because there's no #thing (it doesn't get passed from your controller to your model as an instance variable).
I don't remember how exactly it works, but I'm pretty sure that when calling validate :user_valid, it will pass along the record to be validated for you. If that's indeed the case you could try:
def user_valid
errors.add("you must be logged in to add a thing") unless user_id?
end