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
Related
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!
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>
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 %>
I've got this code in my matches index:
<table>
<% #matches.each do |match| %>
<tr>
<td><%= match.team_a %></td>
<td><%= match.team_b %></td>
<td><%= match.score_a %> - <%= match.score_b %></td>
<td><%= match.field %></td>
<%= render "shared/asterisk_message", :target => [match.team_b, match.team_a] %>
<% end %>
</table>
I want to move the rendered asterisk_message outside the table, but this would mean to put it after the <% end %>, which obviously gives me this error:
undefined local variable or method `match' for #<#:0xb6da40d8>
How can I work it out?
Try:
<table>
<% #matches.each do |match| %>
<tr>
<td><%= match.team_a %></td>
<td><%= match.team_b %></td>
<td><%= match.score_a %> - <%= match.score_b %></td>
<td><%= match.field %></td>
<%= render "shared/asterisk_message", :target => [match.team_b, match.team_a] %>
<% end %>
</table>
<% #matches.each do |match| %>
<%= render "shared/asterisk_message", :target => [match.team_b, match.team_a] %>
<% end %>
I'm following Railscasts Episode #165 Edit Multiple but having issue where when I go to the product index page it is not showing my list of products. I'm getting only the header and the link to new product. Any idea what I did wrong?
By the way I'm using rails 3.2.3
Thank you.
routes.rb
resources :products do
collection do
post :edit_multiple
put :update_multiple
end
end
resources :categories
index.html.erb
<h1>Listing products</h1>
<% form_tag edit_multiple_products_path do %>
<table>
<tr>
<th></th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
<% for product in #products %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%= product.name %></td>
<td><%= product.category.name %></td>
<td><%= product.price %></td>
<td><%= link_to "Edit", edit_product_path(product) %></td>
<td><%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
<p><%= link_to "New Product", new_product_path %></p>
You need <%= form_tag ... instead of <% form_tag .... Without the = the return value (i.e. your form) is discarded rather than added to the output.
I think this is typo.
you forgot to use "=" You should do <%= form_tag edit_multiple_products_path do %>