Rails嵌套属性文件上传错误 - 期望数组(得到Rack :: Utils :: KeySpaceConstrainedParams)

时间:2015-08-06 03:02:31

标签: ruby-on-rails ruby-on-rails-4 image-uploading nested-attributes

我正在尝试使用嵌套属性上传图片,但我收到此错误:

Rack::Utils::ParameterTypeError - expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `project_images_attributes':

每当我编辑项目以添加新图像时。我想如果我添加了child_index: ProjectImage.new.object_id,那就行了

<%= f.simple_fields_for :photos, ProjectImage.new, child_index: ProjectImage.new.object_id do |ff| %>
  <%= ff.label :photo, "Upload Photo" %>
  <%= ff.file_field :photo, multiple: true, name: "project[project_images_attributes][][photo]" %>
<% end %>

编辑:

ProjectImage模型

class ProjectImage < ActiveRecord::Base
  belongs_to :project

  paperclip_opts = {
    :styles => { :large => "800x800>", :medium => "x430>", :frontpage_thumb => "130x95#", :thumb => "150x150#" }, 
    :convert_options => { :all => "-quality 75 -strip" }
  }

  has_attached_file :photo, paperclip_opts
  validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/

end

项目模型

class Project < ActiveRecord::Base
  has_many :project_images, dependent: :destroy

  accepts_nested_attributes_for :project_images, allow_destroy: true
end

1 个答案:

答案 0 :(得分:1)

您需要更改此

<%= f.simple_fields_for :photos, ProjectImage.new, child_index: ProjectImage.new.object_id do |ff| %>

<%= f.simple_fields_for :project_images, ProjectImage.new, child_index: ProjectImage.new.object_id do |ff| %>