Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rails 4使用carrierwave和嵌套表单进行多文件上载_Ruby_Ruby On Rails 4_File Upload_Carrierwave_Nested Forms - Fatal编程技术网

Ruby Rails 4使用carrierwave和嵌套表单进行多文件上载

Ruby Rails 4使用carrierwave和嵌套表单进行多文件上载,ruby,ruby-on-rails-4,file-upload,carrierwave,nested-forms,Ruby,Ruby On Rails 4,File Upload,Carrierwave,Nested Forms,我有一个包含许多图像的目录项,并尝试使用嵌套表单和carrierwave通过一个请求上载所有图像。我还使用应答器、haml和简单表单。 所以,它有点像: item.rb class Item < ActiveRecord::Base has_many :images, dependent: :destroy accepts_nested_attributes_for :images end class Image < ActiveRecord::Base belongs

我有一个包含许多图像的目录项,并尝试使用嵌套表单和carrierwave通过一个请求上载所有图像。我还使用应答器、haml和简单表单。 所以,它有点像:

item.rb

class Item < ActiveRecord::Base
  has_many :images, dependent: :destroy
  accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
  belongs_to :item
  mount_uploader :image, ImageUploader
end
...
def new
  @item = Item.new
  respond_with(@item)
end

def create
  @item = Item.new(item_params)
  @item.save
  respond_with(@item)
end
...
def item_params
  params.require(:item).permit(
    :name, :description, :price,
    image_attributes: [:image]
  )
end
项目\u controller.rb

class Item < ActiveRecord::Base
  has_many :images, dependent: :destroy
  accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
  belongs_to :item
  mount_uploader :image, ImageUploader
end
...
def new
  @item = Item.new
  respond_with(@item)
end

def create
  @item = Item.new(item_params)
  @item.save
  respond_with(@item)
end
...
def item_params
  params.require(:item).permit(
    :name, :description, :price,
    image_attributes: [:image]
  )
end
我是rails新手,它显然没有按照我想要的方式工作。它保存项目并完全忽略所有图像

所以,我想知道,有没有什么方法可以不用像这样的结构来实现我的目标

def create
  @item = Item.new(item_params)
  params[:images].each do |image|
    img = Image.new
    img.image = image
    @item.images << img
  end
  @item.save
  respond_with(@item)
end
def创建
@item=item.new(item_参数)
参数[:images]。每个do | images|
img=Image.new
img.image=图像

@item.images尝试将您的
新方法更改为如下所示

def new
  @item = Item.new
  @item.images.build
  respond_with(@item)
end
此外,在上载多个图像时,请将
项目参数更改为下面的

def item_params
  params.require(:item).permit(:name, :description, :price, images_attributes: [:image => []])
end

所以,我终于找到了答案。我的html表单中有一些错误。 第一个错误非常明显。我曾经

= simple_fields_for :images do |image|
而不是

= f.simple_fields_for :images do |image|
\u form.html.haml中 我读了这篇文章后发现的第二篇 因此,我将嵌套表单更改为:

= f.simple_fields_for :images, Image.new do |image_form|
    = image_form.file_field :image, multiple: true,
                   name: "item[images_attributes][][image]"
正如Pavan所建议的,在myitems\u controller.rb中使用复数形式的图像属性:

def item_params
  params.require(:item).permit(
    :name, :description, :price,
    images_attributes: [:image]
  )
end

仅此而已。

不,结果还是一样。Item已保存,但图像被忽略。仍然不起作用,但我发现:Item和:images是param散列的两个独立元素,所以我的嵌套表单实现可能有问题吗?
name:“Item[images\u attributes][[image]”中的额外
[]
让我大吃一惊