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 %>
Related
Hello Stackoverflow heroes,
I have an users table with the following field:
authorization_id
And i have an authorizations table with just a regular id.
I tried renaming id to authorization_id but it just added another column called authorization_id it didn't change the id column.
What i am trying to achieve to be able to do
<% #user.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.authorization_id.name %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<% if admin? %>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?',
:method => :delete %></td>
<% end %>
</tr>
<% end %>
But it won't work i suspect that authorization_id is has not been linked to the autorizations table. But i can't seem to find out how to do it.
This is my authorization model
class Authorization < ActiveRecord::Base
belongs_to :user
validates :name, presence: true, length: {minimum: 3}
end
and this is a piece of my user model
class User < ActiveRecord::Base
has_one :authorization
Any help will be welcomed,
Kind regards.
It seems this:
<td><%= user.authorization_id.name %></td>
Should simply be this:
<td><%= user.authorization.name %></td>
You don't need _id.
Also, I think your associations are backwards (the belongs_to model should hold the foreign key). Check the Guide.
First of all, Authorization belongs to User, so the foreign key must be in Authorization model, eg we name it user_id.
So what we need to modify is add the foreign key user_id to Authorization model, that is enough.
Then your view is much easier:
<% #user.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.authorization.name %></td> <!-- Change here! -->
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<% if admin? %>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?',
:method => :delete %></td>
<% end %>
</tr>
<% end %>
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 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) %>
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
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.