【问题标题】:Create Drop Down Box in Rails Form在 Rails 表单中创建下拉框
【发布时间】:2013-05-12 04:23:56
【问题描述】:

我正在尝试在 Rails 中创建一个下拉框,但出现错误,我希望有人能解释一下原因。

这是错误:

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

机器人类:

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

表格:

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

【问题讨论】:

  • $ bundle exec rake db:migrate
  • 我已经完成了 db:migrate,你是在建议我包含 bundle exec 吗?
  • 我试过了,还是不行:(

标签: ruby-on-rails ruby forms ruby-on-rails-3.2


【解决方案1】:

为此,您必须将第二个参数作为特殊的 options_for_select 传递。有一个助手可以为您转换哈希。

你应该阅读:

select_tag

option_for_select

试试这个:

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

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2014-09-02
    相关资源
    最近更新 更多