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>"
Related
I have created a rails helper method, So far so good the only problem is it created an unexpected array ouput.
I did try the key value pair using the each method but the array still there.
I'm trying to figure it out how to remove the unexpected array
My application_helper.rb
def bid_items(origin, destination)
item = Item.where(item_deliver_from: origin).where(item_deliver_to: destination).where(shopper_id: current_user)
end
My search_results.html.rb
<%= bid_items(trip.origin, trip.destination).each do |item| %>
<div class="card border-0">
<%= image_tag item.cover_image_url(:cover_image_medium), class: "card-img-top" %>
<div class="card-body">
<h5 class="card-title"><%= item.name %></h5>
</div>
</div>
<% end %>
Though I got the intended results still, there's an unexpected array
check the image
I just want to remove this area
See Red X
Remove = sign from <%= bid_items(trip.origin, trip.destination).each do |item| %>
So, make it <% bid_items(trip.origin, trip.destination).each do |item| %>
I'm trying to figure out a way to always show 5 jobs in my Rails app and then have a link that when clicked will show all of the remaining jobs. Should I do something in my jobs_controller where I'm just getting the first 5 and then getting the rest, or is it better to do it in the view somehow?
<!-- company -->
<div class="vertical-space">
<b>Company Name</b><br />
<div class="sub-text">
<% Job.by_company_count.size.each do |name, count| %>
<div class="indent"><%= link_to name, filtered_jobs_path(company: name) %>
(<%= count %>)
<br>
</div><!--./indent-->
<% end %>
</div><!--./sub-text-->
</div>
To pass data to the view, set an instance variable of the controller, in the corresponding action:
#jobs = Job.by_company_count
And then access and iterate #jobs in view:
<% #jobs.each do |name, count| %>
<div class="indent"><%= link_to name, filtered_jobs_path(company: name) %>
(<%= count %>)
<br>
</div><!--./indent-->
I'm assuming that you have a scope by_company_count defined in Job model!
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 %>
So I'm trying to combine two tables and show the results in order of the start_date.
I've tried a few things but because its technically a nested loop its giving me double results for each item.
The code i currently have is as follows
<% #subcategory = Subcategory.all %>
<% #product = Product.all %>
<% (#product + #subcategory).each do |product, subcategory|%>
<% if product.display_on_home_page and !product.is_highlight_product and !(product == '..') or subcategory.
display_on_home_page and !subcategory.is_highlight_product and !(subcategory == '..')%>
<div class="column_entry">
<%= link_to image_tag(subcategory.image_attachment.url(:normal_page_size)), subcategories_content_url(subcategory.id), :controller=>'subcategories' %>
</div>
<% end %>
<% if product.price_from %>
<div class="column_entry">
<div class="product_special">
<span class="a">From Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% else %>
<div class="column_entry">
<div class="product_special">
<span class="a">Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% end %>
<% end %>
I know this is quite a long an complex statement, its supposed to loop through all of the subcategories and all of the products and display the images, there are also two different ways of displaying the price based on a boolean that says whether the price is a specific amount or it starts from a given price.
at the moment its reading through the loop but its giving me the error
undefined method `is_highlight_product' for nil:NilClass
since this is the first column in the table that is referenced and its breaking here I think that there must be some conflict in its ability to see the information stored in the table.
I'm still quite new to ruby on rails so any help or even just a nudge in the right direction would be very much appreciated.
If you would like more information just ask in the comments and I'll put it up as fast as I can.
The problem here is, when you do something like this:
(#product + #subcategory).each do |product, subcategory|
The local variable product will iterate firstly through products, then through subcategories, and the local variable subcategory will always be nil.
What you can do, a dirty way - check
if product.is_a?(Product)
# do your things
elsif product.is_a?(Subcategory)
# do other things
end
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%>