I have an Invoice.
Invoice contains LineItems(that belongs to Items).
In the invoice, there is a drop down menu to select line items. I need to add two custom attributes: data-quantity and data-price. (HTML5)
I tried to do it following this article: http://www.redguava.com.au/2011/03/rails-3-select-list-items-with-custom-attributes/ But it doesn't work with nested model attributes.
ApplicationHelper
def options_from_collection_for_select_with_attributes(collection, value_method, text_method, attr_name, attr_field, selected = nil)
options = collection.map do |element|
[element.send(text_method), element.send(value_method), attr_name => element.send(attr_field)]
end
selected, disabled = extract_selected_and_disabled(selected)
select_deselect = {}
select_deselect[:selected] = extract_values_from_collection(collection, value_method, selected)
select_deselect[:disabled] = extract_values_from_collection(collection, value_method, disabled)
options_for_select(options, select_deselect)
end
_line_item_fields.html.erb
Product:
<%= f.select(:item_id, options_from_collection_for_select_with_attributes(@items, :id, :name, 'data-quantity', :quantity), {:prompt => 'Select'}, {:class=>'product'}) %>
I get error: undefined method `quantity' for #Item:0x007f53380eb2
The method in the application helper most probably works well, but the problem is that LineItems belong to Item. LineItems have quantity attribute, but Items don't have a quantity attribute.
As you can see from the error, it's looking at Item, but I it really should reference LineItem.