【问题标题】:Choose from foreign keys in rails admin not working从rails admin中的外键中选择不起作用
【发布时间】:2012-08-04 08:14:54
【问题描述】:

以下是我的模型:

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

我该怎么做?

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-04
        • 2010-12-02
        相关资源
        最近更新 更多