I have a custom validation on a model and I'm getting undefined method which makes no sense.
Here's the code
validate :unique_seo_url_from_title
def unique_seo_url_from_title
url = "#{title.parameterize}-#{region.parameterize}-#{country}"
errors.add(:title, "already in use") if SeoMapping.find_by_seo_url(url)
end
Strange thing is if I output the url ie raise url it prints it perfectly so its working.
Hope someone can advise!
Since you didn't pass parameters to the function, it seems that title, region and country are attributes of model for which validation is used. So use self for the object
validate :unique_seo_url_from_title
def unique_seo_url_from_title
url = "#{self.title.parameterize}-#{self.region.parameterize}-#{self.country}"
errors.add(:title, "already in use") if SeoMapping.find_by_seo_url(url)
end
Thanks
undefined method `parameterize' for nil:NilClass
The message means exactly what it says. You have an object which is nil, and you call parameterize on it -- which fails because that method is defined on String, not NilClass.
Check that the values for title and region are present. If not, there's your problem.
Related
I can't call the run method in a class called MySqliteRequest. When I call the method,the error is going out.
in `main': undefined method `run' for nil:NilClass (NoMethodError)
Here some methods of MySqliteRequest
class MySqliteRequest
def print
puts "Type Of Request #{#type_of_request}"
end
def run
print
end
def _setTypeOfRequest(new_type)
if(#type_of_request == :none or #type_of_request == new_type)
#type_of_request = new_type
else
raise "Invalid: type of request already set #{#type_of_request} (new type => #{new_type}"
end
end
end
And main method ↓
def _main()
request = MySqliteRequest.new
request = request.from('nba_player_data.csv')
request = request.select('name')
request = request.where('birth_state', 'Indiana')
request.run
end
_main()
At the point where you call request.run the value for request is nil. This is why you are seeing the error you're given.
This is happening because the line right above it assigns the nil value to the request variable.
You are clearly coming from another language that is not Ruby (some type of C maybe?), by how you've formatted things. It would help for you to get more familiar with Ruby and its idioms. However, best I can tell, you want to do something like this instead:
def _main
request = MySqliteRequest.new
request.from('nba_player_data.csv')
request.select('name')
request.where('birth_state', 'Indiana')
request.run
end
_main()
This assumes you've also defined (and in some cases probably overridden) these methods on your MySqliteRequest Object or Model:
from
select
where
However, please note that the way you're going about this is just completely against how Ruby and Ruby on Rails is designed to work.
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.
I have an app structure with nested routes in which a proposal belongs to a request and a request has many proposals.
When I execute a send_proposal method, I am trying to get it to update the status of the request to which that proposal belongs, but I am getting an error that says undefined method 'request' for true:TrueClass.
My route for this method (not that I think it matters) is:
put "proposal/:id/send_proposal" => "proposals#send_proposal", as: "send_proposal"
Here is my send_proposal method, as found in my proposals_controller:
def send_proposal
#proposal = Proposal.find(params[:id])
ProposalMailer.send_proposal_to_client(#proposal, #proposal.request.user).deliver_now
#proposal = #proposal.update_attributes(status: "Sent to Client")
#proposal.request = #proposal.request.update_attributes(archived: "Proposal Sent to Client") <<<<<<<<<ERROR CALLED ON THIS LINE
flash[:notice] = "Your proposal has been sent to the client!"
end
I have looked at many SO posts for other TrueClass errors, but can't seem to find one with a problem like this. Can anyone see what I'm doing wrong or help me conceptualize what TrueClass errors are generally about?
update_attributes is an alias for update:
update(attributes)
Updates the attributes of the model from the passed-in hash and saves the record, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.
and update returns true or false (the documentation could be a lot more explicit about this) not the updated model instance. So this:
#proposal = #proposal.update_attributes(status: "Sent to Client")
will leave #proposal as true or false and neither of those have an update_attributes method.
Your controller method should look more like this:
def send_proposal
#...
#proposal.update(status: "Sent to Client"))
#proposal.request.update(archived: "Proposal Sent to Client")
#...
end
You probably want to do some error checking on those two update calls too.
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
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.