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.
Related
This is so simple but isnt working. What am I missing?
controlelr
#guide = Guide.friendly.find(params[:guide_id])
#category = #guide.categories.friendly.find params[:id]
#items = #category.category_items
view
<% #items.each do |item| %>
<%= item.category_item_values.value %>
<% end %>
gives the no method error of
undefined method 'value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>
There is a values column in the category_item_values table so I'm not sure what the problem is.
item.category_item_values is the CollectionProxy instance (one might think of it as of an kinda array.)
Each category_item has [likely, you did not provide sufficiently enough info to guess more precisely] many values. If the assumption above is correct, here you go:
<% #items.each do |item| %>
<% item.category_item_values.each do |value| %>
<%= value %> # or maybe (depending on your model) <%= value.value %>
<% end %>
<% end %>
You will have to loop over each of the category_item_values to get the result as this suggests <CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0> that your category_item_value is a association.
So you could do something like
<% item.category_item_values.each do |category_item_value| %>
<%= category_item_value.value %>
<% end %>
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
Here, I have 10 columns i.e., answer1, answer2, answer3, ..., answer10 in the table MgAnswer.
I have to check whether each column value is present or not. Only if it present,then I have to display it in the page.
Im giving column names dynamically within for loop
<% (1..10).each do |i| %>
<% if MgAnswer."answer#{i}".present? %>
<%= MgAnswer."answer#{i}" %>
<% end %>
<% end %>
Im ending up with Syntax error.
You can indeed dynamically invoke methods in ruby, but this is not the syntax. Instead do
<% (1..10).each do |i| %>
<% if MgAnswer.public_send("answer#{i}").present? %>
<%= MgAnswer.public_send("answer#{i}") %>
<% end %>
<% end %>
It should seem like the following:
<% (1..10).each do |i| %>
<%= MgAnswer.send("answer#{i}") %>
<% end %>
Since ruby can't evaluate line as MgAnswer."method". Also you can just skip if condition, because it will be evaluated to empty string "".
I have a rails table called Movies. Movies are being collected and saved from an API which means that some movies may have a release_date and some may not.
All Movies are being displayed on the home page and they are sorted by {|t| - t.release_date.strftime("%Y%m%d").to_i}
<% #movies.sort_by{|t| - t.release_date.strftime("%Y%m%d").to_i}.each do |movie| %>
<% movie.title %>
<% movie.release_date.strftime("%Y") %>
<% end %>
So this code works fine but only as long as the returned movies have a release date. If they don't have a release date assigned, it gives me the following error.
ActionView::Template::Error (undefined method `strftime' for nil:NilClass):
But im only getting this error if the movie has no release_date.
So how can i add an exception to only display films WITH a release_date, where using strftime would no longer be a problem.
I've tried
<% unless movie.release_date.blank? %>
<% #movies.sort_by{|t| - t.release_date.strftime("%Y%m%d").to_i}.each do |movie| %>
<% #movie.title %>
<% #movie.release_date.strftime("%Y") %>
<% end %>
<% end %>
But that doesn't work as it gives an undefined method for 'movie'
You should be able to use reject to reject nil release_date like follows:
<% #movies.reject{ |m| m.release_date.nil? } %>
Another problem is you are using the variable movie as instance variable #movie within your each block.
Try:
<% #movies.reject{ |m| m.release_date.nil? }.sort_by{|t| - t.release_date.strftime("%Y%m%d").to_i}.each do |movie| %>
<% movie.title %>
<% movie.release_date.strftime("%Y") %>
<% end %>
Update:
And yes, as pointed by #NicolasGarnil in his answer, it's better to do these in SQL side than in ruby side. Select only the required records and let database do the sorting. So you could update your code to be something like:
In controller:
#movies = Movie.where('release_date is not null').order('release_date desc');
Then in your view:
<% #movies.each do |movie| %>
<% movie.title %>
<% movie.release_date.strftime("%Y") %>
<% end %>
For performance reasons you should not be using ruby to sort your records. This should be done at a database level.
You should first ensure that the release_date values are persisted in an appropriate format and then just use Movie.order("release_date desc"). Records with null values will be placed at the end of the results.
I just started learning Rails, I'm having trouble trying to format a time. I have a Ticket model which has many contact logs, and I want to format the created_at timestamp in a view. When I use this code:
<% #contacts.each do |contact| %>
<p><%= contact.created_at.strftime("%B %e, %Y") %></p>
<p><%= contact.author %></p>
<p><%= contact.description %><p>
<% end %>
to print out the contacts associated with a ticket, the page displays the error:
undefined method `strftime' for nil:NilClass
However, when I use
<% #contacts.each do |contact| %>
<p><%= contact.created_at %></p>
<p><%= contact.author %></p>
<p><%= contact.description %><p>
<% end %>
a date is printed (something like 2012-08-26 05:16:15 UTC), along with the expected values for author and description.
I've looked at all of the timestamp values for the contact objects in my database, and none have created_at values of anything other than times. Can anyone help me out with this?
Perhaps one of your records has created_at set to nil, Try this:
<p><%= contact.created_at.present? ? contact.created_at.strftime("%B %e, %Y") : contact.created_at%></p>
created_at could return not only string.
Did you tried this:
<%= contact.created_at.to_s.strftime("%B %e, %Y") %>?