2013-05-01 68 views
0

我已經在我的Rails 3應用程序下面的代碼,它應該被顯示一個選擇框,每個asset_type記錄:在Rails 3中選擇菜單?

assets_helper

def asset_type_all_select_options 
    asset_type.all.map{ |asset_type| [asset_type.name, asset_type.id] } 
end 

_form.html.erb(資產)

<%= f.select :asset_type_id, asset_type_all_select_options, :class => "input-text", :prompt => '--Select-----' %> 

,這裏是我的模型:

asset.rb

belongs_to :asset_type 

asset_type.rb

has_many :assets 

使用上面的代碼中,我得到以下錯誤:

undefined local variable or method `asset_type' for #<#<Class:0x007f87a9f7bdf8>:0x007f87a9f77d48> 

難道我做錯了什麼?這種方法不適用於雙桶型號名稱嗎?任何指針將不勝感激!

回答

1

變量在assets_helper文件asset_type沒有定義。您將需要在將它傳遞給輔助方法

def asset_type_all_select_options(asset_type) 
    # ... 
end 

或使用您的控制器(例如@asset_type)定義了一個實例變量。

但是,您可以通過使用#collection_select形式幫助簡化此。

_form.html.erb(資產)

<%= f.collection_select :asset_type_id, AssetType.all, :id, :name, { prompt: '--Select-----' }, class: 'input-text' %> 

看看API來#collection_select瞭解詳情。