0

Rails 3アプリケーションに次のコードがあります。各asset_typeレコードで選択ボックスを表示することになっています:

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>

私は何か間違ったことをしていますか?この方法は、二重バレルのモデル名では機能しませんか? 任意のポインタをいただければ幸いです!

4

1 に答える 1

1

asset_typeassets_helper ファイルの変数が定義されていません。ヘルパーメソッドに渡す必要があります

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' %>

詳細については、#collection_selectの API をご覧ください。

于 2013-05-01T19:50:22.277 に答える