从rails admin中的外键中选择不起作用

时间:2012-08-04 08:14:54

标签: ruby-on-rails ruby-on-rails-3 rails-admin

以下是我的模特:

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。我能够在管理员中创建选项和民意调查,但我无法将选择与民意调查相关联,反之亦然。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

首先, has_many 类名应为复数:

has_many :choices

您应该为要编辑此关联的模型添加 attr_accessible poll_id choice_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

答案 1 :(得分:0)

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