Rails - 从belongs_to构建collection_select标记

时间:2013-06-05 03:38:03

标签: ruby-on-rails-3 collections

我无法让我的collection_select工作..我看过这两个链接,它们的语法相互矛盾,似乎无法工作。我还阅读了Rails文档。

Rails 3 build a select tag with has_many belongs_to association

Undefined method 'merge' for "test":String - Rails 3.1

当我尝试不同的语法时,我得到了不同的错误,但这是我当前的错误。

undefined method `map' for nil:NilClass

模型

class Instruction < ActiveRecord::Base
    attr_accessible :exercise_id, :order, :sentence, :instruction_type_id
    belongs_to :exercise
    belongs_to :instruction_type
end

class InstructionType < ActiveRecord::Base
    has_many :instructions
    has_many :exercises, :through => :instructions
    attr_accessible :name
end

指令控制器

def create
    @instruction = Instruction.new(params[:instruction])
end

指示表

<div class="field">
    <%= f.label :instruction_type_id %><br />
    <%= f.collection_select(:instruction_type_id, @instruction_types, :id, :name, :prompt => 'Please select country') %>

    <%= f.label :order %><br />
    <%= f.number_field :order %>
</div>

谢谢大家。

0 个答案:

没有答案