多态记录未更新



我有这三个多态模型:

Banner.rb

class Banner < ApplicationRecord
has_many :pictures, as: :imageable, dependent: :delete_all
belongs_to :user
...
end

Product.rb

class Product < ApplicationRecord
belongs_to :user
has_many :pictures, as: :imageable, dependent: :delete_all
...
end

Picture.rb

class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
...
end

我在更新bannersproducts的记录时遇到问题。我能够创建一个包含多张图片的记录(例如横幅(。但是,当我尝试使用新图片更新该记录时,记录会更新,但图片不会保存到数据库中。

以下是我对横幅模型的_form.html.erb

<%= simple_form_for @banner, :html => { :multipart => true } do |f| %>
...
<%= f.input :name %>
<%= file_field_tag "images[]", type: :file, multiple: true %>
<%= f.submit "Add Banner" %>
<% end %>

以下是我banners_controller.rb的相关部分:

class BannersController < ApplicationController
before_action :find_banner, only: [:show, :edit, :update, :destroy]
def new
@banner = current_user.banners.build
end
def create
@banner = current_user.banners.build(banners_params)
respond_to do |format|
if @banner.save
if params[:images]
params[:images].each do |image|
@banner.pictures.create(image: image, imageable_id: @banner.id)
end
end
format.html { redirect_to @banner, notice: 'Banner was successfully created.' }
format.json { render json: @banner, status: :created, location: @banner }
else
format.html { render action: "new" }
format.json { render json: @banner.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def update
if @banner.update(banners_params)
redirect_to root_path 
else
render "edit"
end
end
private
def banners_params
params.require(:banner).permit(:name, :pictures)
end
def find_banner
@banner = Banner.find(params[:id])
end
end

我不确定为什么图像没有得到正确更新。调用更新方法时,它成功,我被重定向到root_path,但是在数据库中检查时,图片尚未更新。

下面是更新时传递的参数的示例:

{
"utf8"=>"✓", "authenticity_token"=>"bnTdEljiWFsIPAV92lQvNidrcIhxwh+IP2OdELYey1UEwzFQUeQchSXa10sCtjvgRRupyqhWKmHBI7SpTVDpmQ==", 
"banner"=>{"name"=>"new banner 3"}, 
"images"=>[#<ActionDispatch::Http::UploadedFile:0x007f85acb99e00 @tempfile=#<Tempfile:/var/folders/7r/ypx2k4pd5jxgyy7kt6wzmv_r0000gn/T/RackMultipart20170819-46445-17e2f5i.png>, @original_filename="C#.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="images[]"; filename="C#.png"rnContent-Type: image/pngrn">, #<ActionDispatch::Http::UploadedFile:0x007f85acb99db0 @tempfile=#<Tempfile:/var/folders/7r/ypx2k4pd5jxgyy7kt6wzmv_r0000gn/T/RackMultipart20170819-46445-2hop9q.png>, @original_filename="CSS-img.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="images[]"; filename="CSS-img.png"rnContent-Type: image/pngrn">, #<ActionDispatch::Http::UploadedFile:0x007f85acb99d60 @tempfile=#<Tempfile:/var/folders/7r/ypx2k4pd5jxgyy7kt6wzmv_r0000gn/T/RackMultipart20170819-46445-14xj5wc.png>, @original_filename="CSS.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="images[]"; filename="CSS.png"rnContent-Type: image/pngrn">], 
"commit"=>"Add Banner", "id"=>"3"
}

轻微不一致。检查banners_controller.rb中的banners_params方法

您正在发送"图像",但您期待"图片"。将其更改为:

def banners_params
params.require(:banner).permit(:name, images:[])
end

_form.html.erb,和改变

<%= file_field_tag "images[]", type: :file, multiple: true %>

<%= f.file_field "images", type: :file, multiple: true %>

@banner.update(banners_params)工作。

上述更改还需要您将create方法更改为使用banners_params[:images]而不是params[:images]

最新更新