我正在使用Padrino并尝试在具有许多图像的对象形式内显示图像上传字段。
型号 project.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
控制器 projects.rb
get :new do
@title = pat(:new_title, :model => 'project')
@project = Project.new
@project.images.build
render 'projects/new'
end
部分 projects / _form.haml
= 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
字段。有什么想法吗?
答案 0 :(得分:1)
变化:
= 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