Rails, concatenating 2 variables and display them in view - ruby-on-rails

thanks in advance for your help!
I would like to concatenate 2 variables and display both in one cell in table in the view. More specifically I want to display column "game score" e.g. 5:7 by concatenating #game.team1_score and #game.team2_score.
Can you help out with this one line?
<% #games.each do |game| %><tbody>
<tr>
<td><%= game.id %> </td>
<td><%= game.team1 %></td>
<td><%= game.team2 %></td>
<td><%= game.team1_score ":" game.team2_score %></td>
<td><%= game.updated_at %></td>
<td><%=link_to("Detail", game_path(game.id), class: 'action show') %></td>
<td><%=link_to("Edit", edit_game_path(game.id), class: 'action edit') %></td>
<td><%=link_to("Delete", game_path(game.id), method: :delete, class: 'action delete') %> <br></td>
<% end %>
</tr>

try this!
<td><%= game.team1_score %>:<%= game.team2_score %></td>

Related

Rails Error comparing object with parameter inside view

I have my index.html like this:
<% #alumno_inscriptos.each do |alumno_inscripto| %>
<tr>
<% if (alumno_inscripto.clase_id) == (params[:id]) %>
<td><%= alumno_inscripto.clase_id %><td>
<td><%= params[:id] %><td>
<td><%= buscarNombre(alumno_inscripto.alumno_id).name %> <%= buscarNombre(alumno_inscripto.alumno_id).lastname %> </td>
<td> <%= alumno_inscripto.presencia %></td>
<td> <%= alumno_inscripto.pago %></td>
<td><%= link_to 'Destroy', alumno_inscripto, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<td><%= "no va. solo para testear" %><td>
<td><%= alumno_inscripto.clase_id %><td>
<td><%= params[:id] %><td>
<% end %>
</tr>
<% end %>
The problem is that inside the 'if', if the 2 parameters are the same, it always goes to the else part. I leave you an image of what it print. As you can see in the last row the values are the same. Do you have a solution for this?
Thank you!

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 %>

How to displaying an specific attributes with a many to many association in rails

I have a two way many-to-many assoication between 3 models: work.rb, category.rb, categorywork.rb
Within the work#index using <%= work.categories %> renders some wonky looking html markup
<% #works.each do |work| %>
<tr>
<td><%= work.name %></td>
<td><%= work.subtitle %></td>
<td><%= work.categories %></td>
<td><%= link_to 'Show', work %></td>
<td><%= link_to 'Edit', edit_work_path(work) %></td>
<td><%= link_to 'Destroy', work, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I'm trying to target speific attributes of the association like "name".
Unfortunately when using <%= work.categories.name %> it gets weirder with:
How do i target just the name or just the description?
Try this out:
<%= work.categories.pluck(:name) %>

Rails index list a variable does change during looping

I'm trying to set a button for each Client in an index list that will take you to that Client's Location.
This is my code:
<% Client.active.find_each do |client| %>
<tr>
<td><strong><%= link_to client.client_name, client_path(client) %></strong></td>
<td><%= client.locname %></td>
<td><%= client.phone1 %></td>
<td><%= client.fax %></td>
<td><%= client.worequests.notcompl.count %></td>
<td><%= client.workorders.count %></td>
<td><%= client.contacts.count %></td>
<% location = Location.where('locname' == client.locname).first.id %>
<td><%= link_to 'Tree', location_url(location), :class => 'btn btn-mini btn-primary' %></td>
<% if current_user.has_role? :admin or current_user.has_role? :super %>
<td><%= link_to 'Edit', edit_client_path(client), :class => 'btn btn-mini btn-success' %></td>
<% else %>
<td></td>
<% end %>
</tr>
<% end %>
But, the link is always to location/13 - which is from the first location.where lookup.
Why doesn't each Client's Tree button point to their location?
Thanks for your help!
I think this is your problem:
Location.where('locname' == client.locname)
should be
Location.where(:locname => client.locname)
Your where clause is evaluating to true no matter what. I think you want:
Location.where('locname=?', client.locname).first.id

Resources