Select menus in Rails 3?

I have the following code in my Rails 3 application, it's supposed to be displaying a select box with each asset_type record:

assets_helper

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

_form.html.erb (Asset)

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

and here are my models:

asset.rb

belongs_to :asset_type

asset_type.rb

has_many :assets

Using the above code I get the following error:

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

Am I doing something wrong? Will this method not work with double barrel model names? Any pointers would be appreciated!

Answers


The variable asset_type in your assets_helper file is not defined. You would need to pass it in to the helper method

def asset_type_all_select_options(asset_type)
  # ...
end

Or use an instance variable that you define in the controller (e.g. @asset_type).

However, you can simplify this by using the #collection_select form helper.

_form.html.erb (Asset)

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

Take a look at the API for #collection_select for details.


Need Your Help

UIScrollView using Autolayout will only bounce

ios uiscrollview autolayout uistoryboard

I am really struggling to get a UIScrollView to work correctly with Autolayout. Instead of scrolling down as it should, it just bounces, so if I drag it to see more as soon as I let go it returns t...

why this exception: java.lang.RuntimeException: Error inflating class

android exception inflate

I am trying to develop a tiny android app that just opens a browser to a website. I am using mac OS 10.6 and the latest android/eclipse tools. But I keep getting this exception:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.