I am have a instance variable ride that comes from a table and it has a column ride.pickup_at
If I do <%= ride.pickup_at.class.name %> I get
time
So Strftime should be available.
Now, if I do <%= ride.pickup_at.strftime('%x') %> I get an error
undefined method `strftime' for nil:NilClass
What do I have to do to make strftime available?
you can use try to do the nil judge: <%= ride.pickup_at.try(:strftime, '%x') %> .Or I think you should figure out what to do if ride.pickup_at is nil
You can add a if condition like this before your code like this.
<%if !ride.pickup_at.nil? %>
<%= ride.pickup_at.strftime('%x') %>
<% end %>
This might be because of some record having nil value for pickup_at
Related
I am trying to output just the date on the generated line, but when I execute this code, which I am sure is not elegant at all, it gives me undefined method 'strftime' for nil:NilClass
<% #person.subordinates.each do |sub| %>
<tr>
<td>
<% if sub.position == 'alumni' %>
<p>
<%= link_to(sekret_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %>,</strong>
<% end %> <%= sub.startdate.strftime("%Y") %> - <%= sub.graddate.strftime("%Y") %>
</p>
<% end %>
</td>
</tr>
<% end %>
Further up in my code, I am using #person.startdate.strftime("%m/%d/%Y") without any issue, but I think my issue is that I am using the sub call, which is grabbing any people that are flagged as sub to this person.
Your error says that one of your attributes is nil. The sytax is correct. Try debugging with:
#person.subordinates.select { |sub| sub.startdate.nil? || sub.graddate.nil? }
The elements that are retrieved on this line will be the ones that are giving you errors, because you are trying to run nil.strftime('%Y'), which makes sense.
You should use a condition on your .erb to stop it, like:
<%= sub.startdate && sub.startdate.strftime("%Y") %> - <%= sub.graddate && sub.graddate.strftime("%Y") %>
It is not an error with Rails.
strftime is being called on nil object. Make sure you are calling strftime on a date, time, or datetime object.
Refer to strftime on rails apidock - https://apidock.com/ruby/DateTime/strftime
Another way to solve this that will guard against nils would be:
<%= sub.startdate&.strftime("%Y") %> - <%= sub.graddate&.strftime("%Y") %>
That way you don't have to add a conditional to make sure that any method/attribute in the chain exists prior to calling them. This works because of the safe navigation operator, which is explained in depth here: https://rubyinrails.com/2017/11/17/safe-navigation-operator-ampersand-dot-in-ruby/
I have a loop to getting values in my database, but when I'm trying to get a value from a relationship, I get an error:
undefined method `first_name' for nil:NilClass
This is the loop:
<%= #sample.each do |s| %>
<%= s.relation.first_name %>
<% end %>
when I tried
<%= s.relation.to_json %>
with .to_json, I get this:
{"first_name":"testingtwo","last_name":"fdsaf","updated_at":"2013-11-21T07:47:05Z","user_id":null}
Shouldn't s.relation.first_name work?
Thanks
it's possible that there is a relation in the loop that doesn't have a value, and is getting hung up there - try this:
<%= #sample.each do |s| %>
<%= s.relation.first_name if s.relation %>
<% end %>
this only outputs s.relation.first_name if it isn't nil. You could also try this:
<%= #sample.each do |s| %>
<%= s.relation.first_name || "No name given" %>
<% end %>
which puts the first name if it exists, or "No name given" if it doesn't
One important thing you may want to consider too is whether or not having nil values within your database for that particular relationship is valid to begin with.
The || solution proposed is definitely a good one and a great Ruby pattern, but it may be worth adding in an ActiveRecord validation to ensure your relation(ships) are never nil.
ActiveRecord Validations are definitely the way to go in that particular case.
On my user profile page in my rails app, I am recieving the error:
undefined method `title' for nil:NilClass
I know it is because of the following 3 lines of code:
<% if #user.profile.title %>
<%= #user.profile.title %>
<% end %>
I don't understand why. Since i use the if statement, shouldn't it first check whether title exists, then if it exists display it, and if it does not exist, it should not display it. What is wrong and how do I fix it? Thanks.
Since title is nil you can't test against it like you did.
Try:
<% if #user.profile.try(:title) #user.profile.title %>
You can do:
<% if #user.profile.present? %>
<%= #user.profile.title %>
<% end %>
Or with try:
<%= #user.profile.try(:name) %>
With a default value if no profile associated:
<%= #user.profile.try(:name) || 'No profile for this user' %>
Because it is not correctly. If you want to get access to some parameter from user you have to use direct access. But if you want to use profile that belongs to User model look at this
link
Unable to render category name in the index view, when passing
<%= post.category.name %>
to the
<% #posts.each do |post| %>
Error:
undefined method `name' for nil:NilClass
However, when passing
<%= post.category %>
I get
#<Category:0x007ff5c2c20b68>
Within individual Show actions
<%= #post.category.id %>
works perfectly. What can be the problem?
Thanks
I think that for atleast 1 post, the category is nil
You can avoid the error by making this change
<%= post.category.name if post.category %>
or
<%= post.category.try :name %>
Look at the SELECT commands that are being invoked in each case, you can see it in your terminal where you run rails s
I suspect that for some reason, in the index controller, the categories information is not being retrieved together with the posts
If you don't know what is wrong with the SELECT commands, post both cased here together with the controller and model
If I have a :due column that is a datetime type, how would I make calling .strftime on it work?
For instance, I have
<% #todos.each do |todo|%>
<%= todo.due.strftime("%h %d") %>
<% end %>
in my show view and I get undefined method `strftime' for nil:NilClass
I have this in my show method in the controller:
#todos = ProjectTodo.for_project(params[:id])
Note: I'm not trying to display when it was updated or created, just want to display a formatted date.
try this..
<% #todos.each do |todo|%>
<%= todo.due ? todo.due.strftime("%h %d") : nil %>
<% end %>
If the date is there then it will convert to the given format else it will display nil.