1

以下是我的模型:

class Poll < ActiveRecord::Base
  attr_accessible :published, :title

  validates :published, :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 10 }

  has_many :choice, :dependent => :destroy
end


class Choice < ActiveRecord::Base
  belongs_to :poll
  attr_accessible :choice_text, :votes

  validates :choice_text, :presence => true

end

然后我尝试安装rails admin。我能够在管理员中创建选择和民意调查,但我无法将选择与民意调查相关联,反之亦然。

我该怎么做?

4

2 回答 2

1

首先在has_many中的类名应该是复数:

has_many :choices

您应该为要编辑此关联的模型添加attr_accessible poll_idchoice_ids 。或者只是删除所有attr_accessible进行第一次尝试。

class Poll < ActiveRecord::Base
  attr_accessible :published, :title, choice_ids

  validates :published, :presence => true
  validates :title, :presence => true, :length => { :minimum => 10 }

  has_many :choices, :dependent => :destroy
end


class Choice < ActiveRecord::Base
  belongs_to :poll
  attr_accessible :choice_text, :votes, :poll_id

  validates :choice_text, :presence => true

end
于 2012-12-11T18:45:36.337 回答
0

attr_accessibleRails 4中没有。请accepts_nested_attributes_for改用。更多信息: https ://github.com/sferik/rails_admin/wiki/Belongs-to-association

于 2015-07-08T03:16:51.790 回答