多态记录未更新

问题描述 投票:0回答:1

我有这三个多态的模型:

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

我不确定为什么图像没有得到适当的更新。当调用update方法时,它会成功,我会被重定向到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\"\r\nContent-Type: image/png\r\n">, #<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\"\r\nContent-Type: image/png\r\n">, #<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\"\r\nContent-Type: image/png\r\n">], 
"commit"=>"Add Banner", "id"=>"3"
}
ruby-on-rails ruby paperclip ruby-on-rails-5 polymorphic-associations
1个回答
0
投票

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

你正在发送“图像”,但你期待“图片”。改为:

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]

© www.soinside.com 2019 - 2024. All rights reserved.