hellomello
hellomello

Reputation: 8587

Rails nested attributes file uploading error - expected Array (got Rack::Utils::KeySpaceConstrainedParams)

I'm trying to upload images using nested attributes, but I'm getting this error:

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

Whenever I edit my project to add new images. I thought if I added child_index: ProjectImage.new.object_id, it'll work

<%= 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 %>

Edit:

ProjectImage Model

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

Project Model

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

  accepts_nested_attributes_for :project_images, allow_destroy: true
end

Upvotes: 1

Views: 2352

Answers (1)

Pavan
Pavan

Reputation: 33542

You need to change this

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

to

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

Upvotes: 1

Related Questions