题
我正在使用Padrino,并尝试在具有许多图像的对象的表单中显示图像上传字段。
模型 工程。rb
class Project < ActiveRecord::Base
has_many :images, class_name: 'ProjectImage', foreign_key: 'project_id'
accepts_nested_attributes_for :images, allow_destroy: true
end
模型 project_image。rb
class ProjectImage < ActiveRecord::Base
belongs_to :project
mount_uploader :file, ProjectUploader
end
管制员 项目。rb
get :new do
@title = pat(:new_title, :model => 'project')
@project = Project.new
@project.images.build
render 'projects/new'
end
部分 项目/_form.哈姆尔
= form_for :project, url(:projects, :create), multipart: true, :class => 'form-horizontal' do |f|
- f.fields_for :images do |image_f|
=image_f.label :file, :class => 'control-label'
.controls
=image_f.file_field :file, :class => 'form-control input-xlarge input-with-feedback', :multiple => true
但嵌套 image_f
字段不显示。有什么想法吗?
解决方案
改变:
= form_for :project, url(:projects, :create), multipart: true, :class => 'form-horizontal' do |f|
到
= form_for @project, url(:projects, :create), multipart: true, :class => 'form-horizontal' do |f|
很可能你也需要改变 - f.fields_for
到 = f.fields_for
不隶属于 StackOverflow