<% 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%>
Related
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%>
Sorry for the amateur question but I am still quite new to rails. I currently have an app that creates jobs and I would like to display the jobs beneath the date they were created in the same way they do on Dribble
At the moment to display the jobs I have te following:
<% #jobs.each do |job| %>
<div class="job-wrapper"><%= link_to user_job_path(job.user_id ,job) do %>
<div class="job">
<div class="job-desc"><strong><%= job.company %></strong> are looking for a <strong><%= job.job_title %></strong>
<div class="job-sal"><%= job.job_salary %></div>
</div>
</div>
</div>
<% end %>
<% end %>
I am sure I need to create a loop of some kind to make this work but am unsure how to incorporate it so that the date only displays once at the top of the jobs and then all jobs created during that date are shown?
Any help would be much appreciated! Thanks
Try the pattern below --
<% #job.group_by{|x| x.created_at.to_date }.each do |date,jobs_on_that_date| %>
<%= date %>
<% jobs_on_that_date.each do |job| %>
# render the job
<% end %>
<% end %>
Basically you need to group your jobs by the date (or whatever you want to group on) then get a hash keyed on the stuff you grouped on. Then render the key (date) followed by the list of objects relating to that key.
So let's say we have a rails view with this code:
<h1>Users who have the <%= #title.name %> Title</h1>
<ul>
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-X"></div></li>
<% end %>
</ul>
And what we want to do is to automatically print this line:
<div class="rw-ui-container rw-urid-X"></div>
Right aside each username the loop throws at us BUT we want X to be a different unique random number each time...
How can this be accomplished?
May be this solution can help you:
1. You can use user_id as first part of the random X to ensure that
another user doesn't have it
2. Second part is time in UTC format to set X different each time
3. Third part is just a standart random func. You haven't use it if you want.
<h1>Users who have the <%= #title.name %> Title</h1>
<ul>
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-<%= "#{user_id}#{Time.now.utc.to_i}#{rand(100000)}".to_i %>"></div></li>
<% end %>
</ul>
css_arr = ["rw-ui-container rw-urid-1","rw-ui-container rw-urid-2","rw-ui-container rw-urid-3"]
<div class="<%= css_arr.sample %>"></div>
Well You could easily add a new field to user table call
uuid or give it any name that you like and assign a unique id using the gem called uuid
This help ensure that you have uniqueness in X section and also on refresh the value wont change as you mention in one of your comment
so here how your X code would look like
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-<%=user.uuid %>"></div></li>
<% end %>
I have a model with a lot of attributes, and have build a series of pages to collect all the relevant data. In the last page, I want to show the user all the collected data.
I could create this page by manually typing all the labels and values for each attribute, but I expect that this kind of tedious and repetitive task has already been solved by someone so that in 3-4 lines of code.
At this stage I am only prototyping so this doesn't need to look good.
Anyone has any suggestions as to how to quickly print on the screen all attributes of a model?
I was thinking something like this:
If #my_data_model is the instance variable of which I want to print the attributes, then:
<%= show_attributes #my_data_model %>
would output the attribute values with their labels.
Thanks in anticipation.
I am doing that for one of my projects like this:
First I define an array of the columns I don't want like the timestamp columns:
<% #rejects = ["id", "created_at", "updated_at" %>
Then from the object I remove those columns;
<% #columns = Patient.column_names.reject { |c| #rejects.include?(c) } %>
Then I iterate through the column_names and print out the entered information:
<h2>Is the following information correct?</h2>
<div class="checks">
<h3>Patient details</h3>
<% #columns.each_with_index do |c, i| %>
<p id="p<%= i %>" class="check">
<span class="title"><%= c %>:</span>
<span class="value"><%= #patient[i] %></span>
<span class="valid">
<img src="../../images/icons/tick.png" alt="green tick">
</span>
</p>
<% end %>
</div>
Hope this helps!
I have been using this as a generic show view for inheritated_resources gem.
%h2= resource_class.model_name.human
%table
- resource_class.column_names.each do |column_name|
%tr{ :class => (cycle "odd", "even") }
%td= resource_class.human_attribute_name(column_name)
- if resource[column_name].respond_to?(:strftime)
%td= l resource.send(column_name)
- else
%td= resource.send(column_name)
There resource_class returns the current model class and resource the current instance of it.
Thank you all,
I build a solution based on your recommendations like this:
<% #rejects = ["_id", "created_at", "updated_at"] %>
<% #columns = Agency.column_names - #rejects %>
<% #columns.each_with_index do |c, i| %>
<p id="p<%= i %>" class="check">
<span class="title"><%= c %>:</span>
<span class="value"><%= #agency.send(c) %></span>
</p>
<% end %>
Using <%= #patient[i] %> didn't work for me, probably because I am using Mongomapper as my ORM.