Nested attributes of uploaded image not being save

2019-06-01 07:34发布

问题:

I use Refile to attach images. Given the following models:

class User < ActiveRecord::Base
  has_many :avatars, dependent: :destroy                                                                                                                             
  accepts_attachments_for :avatars, attachment: :file, append: true
  accepts_nested_attributes_for :avatars, :allow_destroy => true
end

class Avatar < ActiveRecord::Base
  belongs_to :user
  attachment :file
end

I have been trying to store the image metadata—in particular, the filename, size, and content type—in the database. To do this, I added the necessary columns to the Avatar table:

class AddMetadataToAvatars < ActiveRecord::Migration
  def change
    add_column :avatars, :avatar_filename, :string
    add_column :avatars, :avatar_size, :integer
    add_column :avatars, :avatar_content_type, :string
  end
end

The metadata get extracted, but are not inserted into the database since only the :id and _:destroy are referenced:

Started PATCH "/users/2" for ::1 at 2015-09-08 23:18:16 +0100
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Nd1e6floj/2E9LIGYQ5k/ySHaoaih7qdfXgeyOPfNvjazg3wwv32hGGRFdaPt2QF+qgPZ2LkKIIOOYhBkkxV+w==", "user"=>{"organization"=>"", "description"=>"Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "avatars_files"=>[#<ActionDispatch::Http::UploadedFile:0x007ffdbb7a7630 @tempfile=#<Tempfile:/var/folders/s_/dyxd936x3y3d0r_vj__rc3fr0000gn/T/RackMultipart20150908-1276-hshsql.jpg>, @original_filename="saner.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatars_files][]\"; filename=\"saner.jpg\"\r\nContent-Type: image/jpeg\r\n">], "avatars_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"11"}}}, "commit"=>"Update", "id"=>"2"}
User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
(0.1ms)  BEGIN
Avatar Load (0.4ms)  SELECT "avatars".* FROM "avatars" WHERE "avatars"."user_id" = $1 AND "avatars"."id" = 11  [["user_id", 2]]
SQL (0.7ms)  UPDATE "users" SET "description" = $1, "organization" = $2, "updated_at" = $3 WHERE "users"."id" = $4  [["description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."], ["organization", ""], ["updated_at", "2015-09-08 22:18:16.467827"], ["id", 2]]
SQL (0.2ms)  DELETE FROM "avatars" WHERE "avatars"."id" = $1  [["id", 11]]
SQL (0.4ms)  INSERT INTO "avatars" ("file_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["file_id", "73a5b77ea364a091c9e88743c0698c6891ea32baf3005e59c2db18d60398"], ["user_id", 2], ["created_at", "2015-09-08 22:18:16.473533"], ["updated_at", "2015-09-08 22:18:16.473533"]]
(0.5ms)  COMMIT
Redirected to http://localhost:3000/users/2
Completed 302 Found in 20ms (ActiveRecord: 2.7ms)

This happens even though I have whitelisted the attributes in the controller:

def user_params
  params.require(:user).permit(:description, :organization, avatars_files: [], avatars_attributes: [:id, :avatar_filename, :avatar_size, :avatar_content_type, :_destroy])
end

Image attachment and removal work fine. The only issue is that the metadata are not added.

I am probably missing something simple here. Any ideas?

回答1:

Given the refile documentation, you need to add the following columns to your avatars table:

file_filename
file_size
file_content_type

Also update the models as follows:

class User < ActiveRecord::Base
  has_many :avatars, dependent: :destroy                                                                                                                             
  accepts_attachments_for :avatars, attachment: :file, append: true
  # you don't need accepts_nested_attributes_for
  # accepts_attachments_for wires up the necessary accepts_nested_attributes_for
  # accepts_nested_attributes_for :avatars, :allow_destroy => true
end

class Avatar < ActiveRecord::Base
  belongs_to :user
  attachment :file
end

And update the whitelisting of user params as follows:

def user_params
  # you dont need to whitelist avatars_attributes
  # avatars_files is enough to be whitelisted
  params.require(:user).permit(:description, :organization, avatars_files: [])
end

Hope that helps.