I have a model, Download, with a HABTM relationship to another model,
Product. In the index view for Download, I want to list all of the
associated products by name. Here is my view code for the table row:
<script src="http://gist.github.com/73492.js"></script>
I've tried using "download.products.join(', ')", but all I get is the
objects has mark (#) in my HTML. Is there a quick and easy way of
creating a joined list of all the product names that I just don't know
about?
Thanks.
Sorry, here's my view code:
<tr class="<%= cycle('off', 'on') %>">
<td><%= link_to(download.title, edit_download_path(download)) %></td>
<td><%= link_to(download.file_name, download_path(download)) %></td>
<td><%= download.part_number %></td>
<td>
<% download.products.each do |product| -%>
<%= product.name %>,
<% end -%>
</td>
<td class="delete"><%= link_to('×', download_path
(download), :confirm => "Are you sure you want to delete #
{download.title}?", :method => 'delete') %></td>
</tr>
I have a model, Download, with a HABTM relationship to another model,
Product. In the index view for Download, I want to list all of the
associated products by name. Here is my view code for the table row:
<script src="http://gist.github.com/73492.js"></script>
I've tried using "download.products.join(', ')", but all I get is the
objects has mark (#) in my HTML. Is there a quick and easy way of
creating a joined list of all the product names that I just don't know
about?
when you call join on products it calls to_s on each Product, because
it doesn't know any better (and the particular details of the default
to_s on objects means that what you get is invalid html that is often
displayed as just # by browers). It's up to you to turn that array of
products into an array of suitable strings (checkout the select/map
methods on Array) and then call join on that
Fred
<%= @download.products.collect{ |p| p.name }.join( ', ' ) %>