我使用重新归档"来附加图像.给出以下模型:
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我一直在尝试将图像元数据(特别是文件名,大小和内容类型)存储在数据库中.为此,我在Avatar表中添加了必要的列:
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提取元数据,但由于仅引用:id和_:destroy,因此未将其插入数据库:
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 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?
推荐答案给出重新归档文档,您需要在avatars表中添加以下列:
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希望有帮助.
更多推荐
上载图片的嵌套属性未保存
发布评论