1

我有一个简单的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 Admin)直接添加图像?

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

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

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

在此处输入图像描述

4

1 回答 1

1

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

例如:

has_many :images, :class_name => 'ProjectImage', :dependent => :destroy, :inverse_of => :project
accepts_nested_attributes_for :images, :allow_destroy => true
于 2013-01-31T20:49:55.847 回答