Ruby on rails 3 选择Rails 3中的菜单?

Ruby on rails 3 选择Rails 3中的菜单?,ruby-on-rails-3,select,model,relationship,Ruby On Rails 3,Select,Model,Relationship,我的Rails 3应用程序中有以下代码,它应该显示一个包含每个asset\u类型记录的选择框: 资产\u助手 def asset_type_all_select_options asset_type.all.map{ |asset_type| [asset_type.name, asset_type.id] } end \u form.html.erb(资产) 资产类型.rb belongs_to :asset_type has_many :assets 使用上述代码,我得到以下错误:

我的Rails 3应用程序中有以下代码,它应该显示一个包含每个
asset\u类型
记录的选择框:

资产\u助手

def asset_type_all_select_options
  asset_type.all.map{ |asset_type| [asset_type.name, asset_type.id] }
end
\u form.html.erb(资产)

资产类型.rb

belongs_to :asset_type
has_many :assets
使用上述代码,我得到以下错误:

undefined local variable or method `asset_type' for #<#<Class:0x007f87a9f7bdf8>:0x007f87a9f77d48>
未定义的局部变量或方法“资产类型”#

我做错什么了吗?此方法是否适用于双桶模型名称?任何提示都将不胜感激

未定义assets\u helper文件中的变量
asset\u type
。您需要将其传递给helper方法

def asset_type_all_select_options(asset_type)
  # ...
end
或者使用在控制器中定义的实例变量(例如
@asset\u type

但是,您可以通过使用
#collection_select
表单帮助器来简化此过程

\u form.html.erb(资产)


有关详细信息,请参阅API

def asset_type_all_select_options(asset_type)
  # ...
end
<%= f.collection_select :asset_type_id, AssetType.all, :id, :name, { prompt: '--Select-----' }, class: 'input-text' %>