Rails select options tag - generating options from associations
I have a view that allows the user to add list_items to one of their
shopping_lists. I want the options for select to be the names of their
lists, but I need those names mapped to respective list_id in order to
have the correct associations. Here's my current select tag:
<tr>
<% item.inventory_items.each do |product| %>
<td>
<%= form_tag("/list_items", method: "post") do %>
<%= hidden_field_tag(:item_id, item.id) %>
<%= hidden_field_tag(:inventory_item_id, product.id) %>
<%= select_tag(:shopping_list_id,
options_for_select(current_user.shopping_lists)) %>
<%= submit_tag("$#{product.price}", class: "btn btn-primary") %>
<% end %>
</td>
<% end %>
</tr>
How can I display the names of a user's shopping_lists as the options, but
return the relative shopping_list_id as the value for that option?
ShoppingList belongs_to :user User has_many :shopping_lists
Currently my select tag renders a dropdown selection of a user's shopping
lists, but the options are listed in the form, '#. Clicking the submit
button doesn't actually add the the item to the list either.
Thanks in advance!
No comments:
Post a Comment