Param丢失或值为空.Rails

时间:2015-05-02 09:02:56

标签: ruby-on-rails

我尝试在app中制作带有carrierwave的多上传图片。有2个课程:DownloaderPictures

Downloader有两个默认字段

t.datetime "created_at"
t.datetime "updated_at"

downloaders_controller

class DownloadersController < ApplicationController

  def new
    @downloader = Downloader.new  
    @picture = @downloader.pictures.build
  end

  def create
    @downloader = Downloader.new(downloader_params)
    if @downloader.save
      params[:pictures]['image'].each do |img|
        @picture = @downloader.pictures.create!(image: img, downloader_id: @downloader.id)
      end
      redirect_to new_downloader_path
    end
  end

  private
  def downloader_params
    params.require(:downloader).permit(pictures_attributes: [ :image ])
  end
end

pictures

  create_table "pictures", force: true do |t|
    t.string   "image"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "downloader_id"
  end

查看new

= form_for @downloader, multiple: true do |f|
  = f.fields_for :pictures do |p|
    = p.file_field :image, multiple: true, name: "pictures[image][]"
  = f.submit

当我创建一个新的downloader对象时,shell会显示错误

param is missing or the value is empty: downloader

如何解决?

1 个答案:

答案 0 :(得分:0)

尝试:

p.file_field :image, multiple: true, name: "downloader[pictures[image][]]"

但是根据经验,你应该避免重写rails惯例并且只是玩它。

此外,通过覆盖这样的输入名称,您的强参数定义可能不再适用于pictures_attributes