RailsAdmin has_many直接创建

时间:2013-01-17 15:20:13

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

我有一个简单的has_many附件情况:

class Project < ActiveRecord::Base
    has_many :images, :class_name => 'ProjectImage', :dependent => :destroy

class ProjectImage < ActiveRecord::Base
    has_attached_file :image
    belongs_to :project

在创建/编辑项目时是否可以(通过Rails管理员)直接添加图像?

现在有两种方法(都很糟糕!):

1)创建/编辑ProjectImage实例并将其添加到项目中(您必须搜索它)。

2)Add a new Project image创建一个模态,然后与1)

相同

enter image description here

1 个答案:

答案 0 :(得分:1)

关键是:嵌套属性,使用accepts_nested_attributes_for

例如:

has_many :images, :class_name => 'ProjectImage', :dependent => :destroy, :inverse_of => :project
accepts_nested_attributes_for :images, :allow_destroy => true