2011-10-12 38 views
0

我有發票。Rails 3.如何將自定義屬性添加到選擇菜單?

發票包含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。

+0

你能告訴我們你到目前爲止嘗試過的代碼嗎? –

+0

感謝您審查我的問題布蘭登:)問題更新。 – leonel

回答

相關問題