BlackHatSamurai
BlackHatSamurai

Reputation: 23483

Create Drop Down Box in Rails Form

I'm trying to create a drop down box in rails and I'm getting an error and I'm hopping that someone can shed some light as to why.

Here is the error:

undefined method `bot_id' for #<Robot:0x007fa1d663cac0>

Robots class:

class Robot < ActiveRecord::Base
  attr_accessible :color1, :color2, :image, :name, :speed, :weapon_damage, :weapon_slots, :bot_id
  ROBOT_TYPES = Hash.new("Mini Bot" => "1", "Micro Bot" => "2", "Macro Bot" => "3")
  ....
  end

Form:

...
<div class="field">
    <%= f.label :bot_id %><br />
    <%= f.select :bot_id, Robot::ROBOT_TYPES%>
  </div>
...

Upvotes: 0

Views: 2348

Answers (1)

Ben Miller
Ben Miller

Reputation: 1484

For this to work, you must pass the second arg as a special options_for_select. There is a helper to convert your hash for you.

You should read:

select_tag

option_for_select

Try this:

<%= select_tag :bot_id, options_for_select(ROBOT_TYPES) %>

Upvotes: 3

Related Questions