未显示嵌套表单对象字段

时间:2014-04-12 12:23:45

标签: ruby padrino

我正在使用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字段。有什么想法吗?

1 个答案:

答案 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