0
发票包含LineItems(属于Items)。
在发票中,有一个下拉菜单可供选择订单项。我需要添加两个自定义属性:数据量和数据价格。 (HTML5)
我试图按照以下这篇文章:http://www.redguava.com.au/2011/03/rails-3-select-list-items-with-custom-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'}) %>
我得到错误:未定义的方法`量”的#Item:0x007f53380eb2
在应用助手的方法最有可能效果很好,但问题在于LineItem属于Item。 LineItems具有数量属性,但项目没有数量属性。
正如你从错误中看到的,它看着Item,但我真的应该引用LineItem。
你能告诉我们你到目前为止尝试过的代码吗? –
感谢您审查我的问题布兰登:)问题更新。 – leonel