Block List Question - ruby-on-rails

I have the following code in one of my views:
<% #videos.each do |i| %>
<tr class="<%= cycle("even","odd") %>">
<td><%= i.title %></td>
<td><%= i.premiere %></td>
<td><%= i.film_type %></td>
<td><%= i.preferred_date %></td>
<td><%= i.actual_date %></td>
<td><%= i.created_at %></td>
<td><%= i.updated_at %></td>
<td><%= i.size %></td>
</tr>
<% end %>
It is listing all of the items in a table (which is then sortable) from each video. I want to make the title link to the video that the title belongs to. Could someone please show me how to make i.title into a link? I tried lots of formats and none of them seem to work.
Thank you!

To link to Rails' standard "show" action for the video:
<td><%= link_to(i.title, video_path(i)) %></td>
This assumes that i.class == Video and you have map.resources :videos in your routes.

Related

Loop multiple variable to form one table

What would be the best method to loop multiple variables into one table? Each variable could have one or more data points. For example, I wish to create something like this:
The following code example is only placing each item in a row.
<table class="table">
<% (#variable1 + #variable2 + #variable3).each do |data, v2, v3| %>
<tr>
<% if data.is_a?(Variable1) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% elsif data.is_a?(Variable2) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% elsif data.is_a?(Variable3) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% end %>
</tr>
<% end %>
</table>
In this case, since you don't know how many records might be in each array, I would find out the greatest length of the arrays I have.
In your controller for this aciton could do something like:
#max_length = [#variable_1, #variable_2, #variable_3].map(&:size).max
*Note: this assumes those variables are always arrays, even if they only have a length of 1.
Then in your view file you could do this:
<table class="table">
<% #max_length.times do |n| %>
<tr>
<td><%= #variable_1[n].try(:date) %></td>
<td><%= #variable_1[n].try(:name) %></td>
<td><%= #variable_2[n].try(:date) %></td>
<td><%= #variable_2[n].try(:name) %></td>
<td><%= #variable_3[n].try(:date) %></td>
<td><%= #variable_3[n].try(:name) %></td>
</tr>
<% end %>
</table>
This way, for the third row, for example, in columns 1 and 3 in your example there will be no values, but values for the second column, which has a third record, will appear.

Image_tag First Image

Carrierwave use, I would like to show the first picture of a publication.
Try the following:
<% #autos.each do |auto| %>
<tr class="list">
<td><%= image_tag auto.fotos.first.to_s , size: "50x50" %></td>
<td><%= auto.ciudad %></td>
<td><%= auto.marca %></td>
<td><%= auto.modelo %></td>
<td><%= auto.version %></td>
<td><%= auto.año %></td>
<td><%= auto.hp %></td>
<td><%= auto.km %></td>
<td><%= link_to 'Show', auto %></td>
<td><%= link_to 'Edit', edit_auto_path(auto) %></td>
<td><%= link_to 'Destroy', auto, method: :delete, data: { confirm: 'Are?' }%></td>
</tr>
<% end %>
This makes it look like hexadecimal, not illustrated.
Any ideas?
You need to pass the image_tag an image URL. Instead of calling to_s on the model, you need to call url on the uploader attribute.
auto.fotos.first.foto.url

Iterating in rails view over multiple variables

I'm accessing attributes from several different variables of several different models. I'm trying to find the best way to display these attributes in a table, and I'm getting some unwanted duplication in my view. Here's the relevant part of my table.
<% #list_items.each do |l| %>
<% #i_items.each do |i| %>
<% #details.each do |d| %>
<% #vends.each do |v| %>
<tr>
<td><%= d.product %></td>
<td><%= d.brand %></td>
<td><%= d.details %></td>
<td><%= i.price %></td>
<td><%= v.name %></td>
<td><%= v.address %></td>
<td><%= button_to "Delete", {:controller => :list_items,
:action => 'destroy',
:id => l.id},
:method => :delete %></td>
</tr>
<% end %>
<% end %>
<% end %>
<% end %>
This currently duplicates the rows I want to view by 4x (presumably because I've got 4 do blocks going on and am not properly using them to achieve my goal. Any tips on how to make this work and what I'm do-ing wrong (sorry couldn't help myself)? Also open to suggestions about how to do this a bit more cleanly than my silly way of using 4 variables? Thanks in advance!
So what I'm getting from this is you have these 4 lists that you want to iterate over at once? If that is the issue, do something like this:
<% 0.upto(#list_items.count) do |i| %>
<tr>
<td><%= #details[i].product %></td>
<td><%= #details[i].brand %></td>
<td><%= #details[i].details %></td>
<td><%= #i_items[i].price %></td>
<td><%= #vends[i].name %></td>
<td><%= #vends[i].address %></td>
<td><%= button_to "Delete", {:controller => :list_items,
:action => 'destroy',
:id => #list_items[i].id},
:method => :delete %></td>
</tr>
<% end %>
This is making the assumption that all the arrays are the same length and that order has not been altered. This is not really a safe idea and redesigning your models might be something worth looking into.
Thanks to the suggestion of #kristenmills I sort of realized that I had the necessary associations to do this really cleanly with the below code. Someone probably would've pointed this out had I posted all of my associations and given a bit more background.
<% #list_items.each do |l| %>
<tr>
<td><%= l.item.product %></td>
<td><%= l.item.brand %></td>
<td><%= l.item.details %></td>
<td><%= l.inventory_item.price %></td>
<td><%= l.inventory_item.vendor.name %></td>
<td><%= l.inventory_item.vendor.address %></td>
<td><%= button_to "Delete", {:controller => :list_items,
:action => 'destroy',
:id => l.id},
:method => :delete %></td>
</tr>
<% end %>

Getting info to insert from a belongs_to relationship

I've got orders, which have many line items, and line items which belong to products. I'm trying to extract the product title, but it's not appearing in the output. I'm getting the line_item info fine. They are all linked in the line_items table, which has both order_id and product_id fields. I'm pretty new to rails, please can someone help me figure out where I'm going wrong?
<% #order.line_items.each do |line_item| %>
<tr>
<% line_item.product do |product| %>
<td><%= product.title %></td>
<% end %>
<td><%= number_to_currency(line_item.price) %></td>
<td><%= line_item.quantity %></td>
<td><%= number_to_currency((line_item.price*line_item.quantity))%></td>
</tr>
<% end %>
Try:
<% #order.line_items.each do |line_item| %>
<tr>
<td><%= line_item.product.title %></td>
<td><%= number_to_currency(line_item.price) %></td>
<td><%= line_item.quantity %></td>
<td><%= number_to_currency((line_item.price*line_item.quantity))%></td>
</tr>
<% end %>

Rails 3.1 associations?

I have a Rails 3.1.1 application with the following models:
Company
Member
The two models have the following associations:
Company - has_many :members
Member - belongs_to :company
When adding members I can enter the company ID number and the record is linked successfully, I can lookup members through the company etc.
When I am working on the member show view I would like to 'pull' in details of the company.
Currently I have the following in the show view:
<h1>Listing members</h1>
<table>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Email</th>
<th>Qualifications</th>
<th>Membership</th>
<th>Company</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #members.each do |member| %>
<tr>
<td><%= member.name %></td>
<td><%= member.mobile %></td>
<td><%= member.email %></td>
<td><%= member.qualifications %></td>
<td><%= member.membership %></td>
<td><%= #member.company.company_id %></td>
<td><%= link_to 'Show', member %></td>
<td><%= link_to 'Edit', edit_member_path(member) %></td>
<td><%= link_to 'Destroy', member, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
How do I go about pulling in a field from the related company? For example the company model has two fields (latitude and longitude).
Your loop should look like this:
<% #members.each do |member| %>
<tr>
<td><%= member.name %></td>
<td><%= member.mobile %></td>
<td><%= member.email %></td>
<td><%= member.qualifications %></td>
<td><%= member.membership %></td>
<td><%= member.company_id %></td>
<td><%= member.company.latitude %></td>
<td><%= link_to member.company.name, member.company %></td>
<td><%= link_to 'Show', member %></td>
<td><%= link_to 'Edit', edit_member_path(member) %></td>
<td><%= link_to 'Destroy', member, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
company_id is defined on the Member model, you cannot access it through member.company.company_id. To access a field on the related company model, use member.company.my_field.
These will only work in the members loop, as they access the |member| variable which is passed to the block.
#member.company.latitude
et cetera. Please let me know if you want clarification or more information.

Resources