【问题标题】:Rails nested attributes file uploading error - expected Array (got Rack::Utils::KeySpaceConstrainedParams)Rails 嵌套属性文件上传错误 - 预期数组(得到 Rack::Utils::KeySpaceConstrainedParams)
【发布时间】:2015-10-29 00:47:30
【问题描述】:

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

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

【问题讨论】:

  • 请贴出相关型号代码
  • @Pavan 嗨!我用模型编辑:D
  • 你需要把f.simple_fields_for :photos改成f.simple_fields_for :project_images
  • @Pavan 并删除其余的东西?如果我这样做并编辑项目,它会根据我上传的图像数量显示字段数。所以如果我之前上传了 2 张图片,它会添加 2 个输入字段。
  • 我的意思是把:photos改成:project_images

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


【解决方案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| %>

【讨论】:

    猜你喜欢
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多