多态记录未更新

时间:2017-08-19 11:29:50

标签: ruby-on-rails ruby paperclip ruby-on-rails-5 polymorphic-associations

我有这三种多态的模型:

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\"\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"
}

1 个答案:

答案 0 :(得分: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]