I'm new to Ruby on Rails, and I'm trying to figure out how to combine queries into an array, arrange them in order so they can output based on time.
In my controller I have this:
#tag_media_items = client.tag_recent_media('cats')
#location_media_items = client.location_recent_media('412421')
In my views I have this:
<% #tag_media_items.each do |media| %>
<img src="<%= media.images.thumbnail.url%>">
<% end%>
<% #location_media_items.each do |media| %>
<img src="<%= media.images.thumbnail.url%>">
<% end%>
I can also figure out the time that it was created by this:
<%= media.created_time %>
on each of the loop.
Now I'm learning how to put the loop into an array and then arrange it based on created_item?
Is there a simple syntax for this?
Thanks!
<% (#tag_media_items+#location_media_items).each do |media| %>
<img src="<%= media.images.thumbnail.url%>">
<% end%>
For sorting:
<%(#tag_media_items+#location_media_items).
sort{|a,b| a.created_at <=> b.created_at}.
each do |media| %>
If it is the same model I would suggest to write 1 query, instead of 2 queries.
#media_items = client.recent_media_of_tag_and_location('cats', '412421')
If you still need 2 queries and do sorting you can do like this,
Controller:
#media_items = #tag_media_items | #location_media_items
#sorted_items = #media_items.sort_by &:created_at
View:
<% #sorted_items.each do |media| %>
<img src="<%= media.images.thumbnail.url%>">
<% end%>
Related
<% if product.created_at %>
<span class="label label-warning">New!</span>
<% end%>
Web dev newbie.. Can't figure out how to use if statement in rails view to display the span class when product records is up to 3 days old. Any ideas?
I can query in my console with
variable = Product.where(created_at: 3.days.ago..Time.now)
and get the the correct records. But cannot understand how to translate this into rails.
The ERB block is going into a _partial
You can try this:
<% if product.created_at > 3.days.ago.beginning_of_day %>
<span class="label label-warning">New!</span>
<% end%>
I have an array attribute in the form and wanting to iterate over this and append each index value to dom id.
I've looked up in the API for use of dom_id but I can't locate one that deals with array attribute.
If dom_id is not meant for working with array index, is there any other way to get array indexes in the view?
<div id="ingredient<%= dom_id(?) %>">
I guess you want something like following
<% array.each_with_index do |arr, index| %>
<div id="ingredient<%= index %>">
<% end %>
<% custom_array.each_with_index do |item,index| %>
<div id="<%= ingredient_#{index} %>">
<!--other contents goes here -->
</div>
<% end %>
Otherwise you can try id = " ingredient_<%= index>"
I want to print out a list of links separated by commas in Rails.
Heres what I've got:
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
,
<% end %>
Heres what I want:
Thing A,
Thing B,
Thing C
But right now I get an extra comma on the last iteration of the loop! What should I do?
One way of doing this is with map then Array#join:
<%= topics.map { |topic| link_to(topic.name, topic.link) }.join(',').html_safe %>
if you want to do minimum possible change to your code, you can use the following
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
<% if(topic != topics.last) %>
,
<% end %>
<% end %>
How about using each_with_index, and only put comma before the content unless it's not the first item.
<% topics.each_with_index do |topic, i| %>
<% if i > 0 %>
,
<% end %>
<%= topic.name %>
<% end %>
I made it in one line call (for active records collections) using the concat helper:
<% concat (',') if e.bills.last != b %>
concat is an ERB helper (TextHelper) to add some HTML without the <%= %> syntax, helpful to add few characters.
Here is the full code to make it clear:
<% event.bills.each do |b| %>
<%= link_to(b.number.to_s, bill_display_path(b)) %>
<% concat (',') if e.bills.last != b %>
<% end %>
Simply try this. It works for me
<%= topics.map{|p| p.topic.name}.join(",") %>
You can do the following to print out the comma for all items except for the last:
<% topics.each do |topic| %>
<%= topic %>
<%= "," if topic != topics.last %>
<% end %>
This will check if the current item in the loop is the last item, and will use the <%= %> syntax to output the comma.
I am a Rails noob. I am generating a menu in an ERB template, looping through the array "elements":
<% #elements.each do |element| -%>
<div class="category <%= element.category.color %>">
</div>
<% end %>
I want to add an additional class to each item (except the first, obviously), referencing the element.category.color of the PREVIOUS item, so that the final mark-up looks like:
<div class="category blue">I am the first, no extra class</div>
<div class="category green after-blue">I come after blue</div>
<div class="category yellow after-green">I come after green</div>
This is all basically a work-around to avoid using next-sibling CSS selectors, which are slow as hell in some of the browsers I need to support, and causing rendering problems when changing the background colours of the items.
Can I add the class I want directly in the ERB, or will I need to also use the controller to calculate it?
You can do this
<% #elements.each_with_index do |element, index| -%>
<div class=<%= "category #{element.category.color} " + ("after-#{#elements[index-1].category.color}" unless index == 0) %>>
</div>
<% end %>
Another way:
<% prev = '' %>
<% #elements.each do |element| -%>
<div class=<%= "category #{element.category.color}#{' after-' + prev if prev.present?}" %>>
<% prev = element.category.color %>
</div>
<% end %>
How would one go about turning the following code into the latter?
<div id="faqs">
<% if #faqs.length > 0 %>
<% #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
<% else %>
<p>No FAQs to display.</p>
<% end %>
</div>
<div id="faqs">
<% #faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
</div>
I'm curious as to whether I can get the latter code to work. The only element of it that is failing at the moment is that the content_tag() is not displaying - this is due to the fact that I'm not using printable ruby tags (<%= # %>) but using them will dump out the FAQ objects underneath the content.
I considered the use of puts() to print the content_tag() while inside the ruby tags but that didn't work.
I've tried to search for this issue but haven't yielded anything useful.
Is this achievable and if so, does it have any benefits other than being prettier?
One way to make the later code to work if you can put the body of the loop in a helper function and return the out put of content_tag from that. The line in view file might be somewhat like this.
<%= #faqs.empty? ? content_tag(:p, "No FAQs to display.") : printList(#faqs) %>
and your printList function will return the output of nested content_tags. You can make a generic list printing function which can be used for any list.
Something so obvious but still shared.
This should work (for clarity, I moved FAQ tag generation in separate helper method):
<div id="faqs">
<%= raw (#faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.map { |faq| faq_div(faq) }.join) %>
</div>
or, perhaps more clean:
<div id="faqs">
<%= content_tag(:p, "No FAQs to display.") if #faqs.empty? %>
<%= raw #faqs.map { |faq| faq_div(faq) }.join %>
</div>
meanwhile, in helpers:
def faq_div(faq)
'<div class="faq"><strong>Q:</strong> %s<br /><strong>A:</strong> %s</div>' % [faq.question, faq.answer]
end
This should work:
<% if #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end.empty? %>
<p>No FAQs to display.</p>
<% end %>