- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有图片模型,它存储来自整个应用程序的图片,在我存储图像所有者对象的可成像列中。我希望能够以不同的分辨率保存这些图片。
class Picture < AbstractModel
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader
end
class User < AbstractModel
has_one :picture, class_name: Picture, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :picture
end
class Event < AbstractModel
has_one :picture, class_name: Picture, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :picture
end
class ImageUploader < CarrierWave::Uploader::Base
version :xs if: :for_user? do
process resize_to_fit: [100, 100]
end
private
def for_user?(_picture)
# And here I hit the problem!
# model.is_a? User
end
end
model
模型上的
Picture
变量引用,我已经尝试了所有带有
pry
的变量,但没有成功。
inverse_of
,但它没有帮助。
class Picture < AbstractModel
belongs_to :imageable, polymorphic: true, inverse_of: :pictures
mount_uploader :image, ImageUploader
class User < AbstractModel
has_one :picture, class_name: Picture, as: :imageable, dependent: :destroy, inverse_of: :imageable
accepts_nested_attributes_for :picture
model
方法中
for_user?
的状态
[5] pry(#<ImageUploader>)> ap model
Изображение для {
:id => nil,
:image => #<ImageUploader:0x005583dee8f938 @model=#<Picture id: nil, image: nil, imageable_id: nil, imageable_type: nil, created_at: nil, updated_at: nil>, @mounted_as=:image, @cache_id="1463023347-21567-5096", @filename="rootp_HE_pause-20160113_141810.jpg", @original_filename="rootp_HE_pause-20160113_141810.jpg", @file=#<CarrierWave::SanitizedFile:0x005583dee8cf30 @file="/home/kvokka/proj/volunteers/public/uploads/tmp/1463023347-21567-5096/rootp_HE_pause-20160113_141810.jpg", @original_filename=nil, @content_type="image/jpeg">, @versions={:mini=>#<ImageUploader::Uploader47012442238420:0x005583dee724a0 @model=#<Picture id: nil, image: nil, imageable_id: nil, imageable_type: nil, created_at: nil, updated_at: nil>, @mounted_as=:image>, :xs=>#<ImageUploader::Uploader47012435752020:0x005583dee72450 @model=#<Picture id: nil, image: nil, imageable_id: nil, imageable_type: nil, created_at: nil, updated_at: nil>, @mounted_as=:image>, :avatar=>#<ImageUploader::Uploader47012437005860:0x005583dee72428 @model=#<Picture id: nil, image: nil, imageable_id: nil, imageable_type: nil, created_at: nil, updated_at: nil>, @mounted_as=:image>}>,
:imageable_id => nil,
:imageable_type => nil,
:created_at => nil,
:updated_at => nil
class ImageUploader < CarrierWave::Uploader::Base
version :xs do
process resize_to_fit: [100, 100]
end
version :avatar, if: :for_user? do
process resize_to_fit: [360, 360]
end
private
def for_user?(_)
model.imageable.is_a? User
end
end
has_one
关系的情况下,它也可以在没有 #inverse_of 方法的情况下工作(这对我来说很棘手)。所以,日志:
[43] pry(main)> ap v = VolunteerCenter.create(title: 'tst', address:'tst', city: City.first, phone:'123456',email: 'mm@mm.ru', description: 'ololo', link: 'http://ddd.ru', picture: (Picture.new( remote_image_url: 'https://retina.news.mail.ru/prev780x440/pic/e5/35/image25749462_adfc024a9b54b718c1a755445661b099.jpg')))
City Load (0.8ms) SELECT "cities".* FROM "cities" ORDER BY "cities"."id" ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO "volunteer_centers" ("title", "address", "city_id", "phone", "email", "description", "link", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["title", "tst"], ["address", "tst"], ["city_id", 1], ["phone", "123456"], ["email", "mm@mm.ru"], ["description", "ololo"], ["link", "http://ddd.ru"], ["created_at", "2016-05-12 12:13:44.417945"], ["updated_at", "2016-05-12 12:13:44.417945"]]
SQL (0.3ms) INSERT INTO "pictures" ("image", "imageable_type", "imageable_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["image", "image25749462_adfc024a9b54b718c1a755445661b099.jpg"], ["imageable_type", "VolunteerCenter"], ["imageable_id", 11], ["created_at", "2016-05-12 12:13:44.421458"], ["updated_at", "2016-05-12 12:13:44.421458"]]
(2.2ms) COMMIT
tst {
:id => 11,
:title => "tst",
:created_at => Thu, 12 May 2016 12:13:44 UTC +00:00,
:updated_at => Thu, 12 May 2016 12:13:44 UTC +00:00,
:address => "tst",
:city_id => 1,
:phone => "123456",
:email => "mm@mm.ru",
:description => "ololo",
:vk_link => nil,
:link => "http://ddd.ru"
}
=> nil
[44] pry(main)> ap v.picture
VolunteerCenter Load (0.6ms) SELECT "volunteer_centers".* FROM "volunteer_centers" WHERE "volunteer_centers"."id" = $1 LIMIT 1 [["id", 11]]
Изображение для tst {
:id => 120,
:image => #<ImageUploader:0x0055596544d928 @model=#<Picture id: 120, image: "image25749462_adfc024a9b54b718c1a755445661b099.jpg", imageable_id: 11, imageable_type: "VolunteerCenter", created_at: "2016-05-12 12:13:44", updated_at: "2016-05-12 12:13:44">, @mounted_as=:image, @cache_id=nil, @filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @file=#<CarrierWave::SanitizedFile:0x00555963f0c690 @file="/home/kvokka/proj/volunteers/public/uploads/picture/image/120/image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename=nil, @content_type="image/jpeg">, @versions={:mini=>#<ImageUploader::Uploader46921211280280:0x00555966c55890 @model=#<Picture id: 120, image: "image25749462_adfc024a9b54b718c1a755445661b099.jpg", imageable_id: 11, imageable_type: "VolunteerCenter", created_at: "2016-05-12 12:13:44", updated_at: "2016-05-12 12:13:44">, @mounted_as=:image, @parent_cache_id="1463055224-8966-4735", @cache_id=nil, @filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @file=#<CarrierWave::SanitizedFile:0x00555966e89620 @file="/home/kvokka/proj/volunteers/public/uploads/picture/image/120/mini_image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename=nil, @content_type="image/jpeg">, @versions={}, @storage=#<CarrierWave::Storage::File:0x00555966e8a4a8 @uploader=#<ImageUploader::Uploader46921211280280:0x00555966c55890 ...>>>, :xs=>#<ImageUploader::Uploader46921211274220:0x00555966c55868 @model=#<Picture id: 120, image: "image25749462_adfc024a9b54b718c1a755445661b099.jpg", imageable_id: 11, imageable_type: "VolunteerCenter", created_at: "2016-05-12 12:13:44", updated_at: "2016-05-12 12:13:44">, @mounted_as=:image, @parent_cache_id="1463055224-8966-4735", @cache_id=nil, @filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @file=#<CarrierWave::SanitizedFile:0x005559659d60c0 @file="/home/kvokka/proj/volunteers/public/uploads/picture/image/120/xs_image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename=nil, @content_type="image/jpeg">, @versions={}, @storage=#<CarrierWave::Storage::File:0x005559659d7010 @uploader=#<ImageUploader::Uploader46921211274220:0x00555966c55868 ...>>>, :avatar==>#<ImageUploader::Uploader46921211257860:0x00555966c55840 @model=#<Picture id: 120, image: "image25749462_adfc024a9b54b718c1a755445661b099.jpg", imageable_id: 11, imageable_type: "VolunteerCenter", created_at: "2016-05-12 12:13:44", updated_at: "2016-05-12 12:13:44">, @mounted_as=:image, @parent_cache_id="1463055224-8966-4735", @cache_id=nil, @filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename="image25749462_adfc024a9b54b718c1a755445661b099.jpg", @file=#<CarrierWave::SanitizedFile:0x00555963379328 @file="/home/kvokka/proj/volunteers/public/uploads/picture/image/120/avatar=_image25749462_adfc024a9b54b718c1a755445661b099.jpg", @original_filename=nil, @content_type="image/jpeg">, @versions={}, @storage=#<CarrierWave::Storage::File:0x0055596337a390 @uploader=#<ImageUploader::Uploader46921211257860:0x00555966c55840 ...>>>}, @storage=#<CarrierWave::Storage::File:0x00555965d85fb8 @uploader=#<ImageUploader:0x0055596544d928 ...>>>,
:imageable_id => 11,
:imageable_type => "VolunteerCenter",
:created_at => Thu, 12 May 2016 12:13:44 UTC +00:00,
:updated_at => Thu, 12 May 2016 12:13:44 UTC +00:00
}
class ImageUploader < CarrierWave::Uploader::Base
version :avatar, if: :for_user? do
puts 'This line never run'
process resize_to_fit: [360, 360]
end
private
def for_user?(picture)
binding.pry
model.imageable.is_a? User
end
User
实例中没有准备好,在最后一个之后回调不起作用。
[46] pry(main)> ap u = User.create(email: 'example@test.com', password: '123456', password_confirmation: '123456', picture: (Picture.create remote_image_url: 'http://onrails.club/uploads/default/29/31f7627609164af8.png'))
From: /home/kvokka/proj/volunteers/app/uploaders/image_uploader.rb @ line 68 ImageUploader#for_user?:
66: def for_user?(_)
67: binding.pry
=> 68: model.imageable.is_a? User
69: end
@cache_id "1463060913-8966-3349"
@file #<CarrierWave::SanitizedFile:0x00555963268330 @file="/hom...
@filename "31f7627609164af8.png"
@model #<Picture id: nil, image: nil, imageable_id: nil, imageab...
@mounted_as :image
@original_filename "31f7627609164af8.png"
@versions {:mini=>#<ImageUploader::Uploader46921199022860:0x0055596...
[1] pry(#<ImageUploader>)>
From: /home/kvokka/proj/volunteers/app/uploaders/image_uploader.rb @ line 68 ImageUploader#for_user?:
66: def for_user?(_)
67: binding.pry
=> 68: model.imageable.is_a? User
69: end
@cache_id "1463060913-8966-3349"
@file #<CarrierWave::SanitizedFile:0x00555963268330 @file="/hom...
@filename "31f7627609164af8.png"
@model #<Picture id: nil, image: nil, imageable_id: nil, imageab...
@mounted_as :image
@original_filename "31f7627609164af8.png"
@versions {:mini=>#<ImageUploader::Uploader46921199022860:0x0055596...
[1] pry(#<ImageUploader>)>
(0.3ms) BEGIN
SQL (0.7ms) INSERT INTO "pictures" ("image", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["image", "31f7627609164af8.png"], ["created_at", "2016-05-12 13:48:40.071387"], ["updated_at", "2016-05-12 13:48:40.071387"]]
From: /home/kvokka/proj/volunteers/app/uploaders/image_uploader.rb @ line 68 ImageUploader#for_user?:
66: def for_user?(_)
67: binding.pry
=> 68: model.imageable.is_a? User
69: end
@cache_id nil
@file #<CarrierWave::SanitizedFile:0x005559639c6348 @file="/hom...
@filename "31f7627609164af8.png"
@model #<Picture id: 112, image: "31f7627609164af8.png", imageab...
@mounted_as :image
@original_filename "31f7627609164af8.png"
@storage #<CarrierWave::Storage::File:0x00555963b5add0 @uploader=#...
@versions {:mini=>#<ImageUploader::Uploader46921199022860:0x0055596...
[1] pry(#<ImageUploader>)>
(2.9ms) COMMIT
(0.2ms) BEGIN
User Exists (0.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'example@test.com' LIMIT 1
SQL (0.4ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "example@test.com"], ["encrypted_password", "$2a$11$qq8tVgzISJguKz7oVJWyVO45vW/ujSEsi/ow29w78nC9ByOdjrPYK"], ["created_at", "2016-05-12 13:48:41.761836"], ["updated_at", "2016-05-12 13:48:41.761836"]]
SQL (0.4ms) UPDATE "pictures" SET "imageable_type" = $1, "imageable_id" = $2, "updated_at" = $3 WHERE "pictures"."id" = $4 [["imageable_type", "User"], ["imageable_id", 31], ["updated_at", "2016-05-12 13:48:41.764681"], ["id", 112]]
(2.7ms) COMMIT
, {
:id => 31,
:email => "example@test.com",
:encrypted_password => "$2a$11$qq8tVgzISJguKz7oVJWyVO45vW/ujSEsi/ow29w78nC9ByOdjrPYK",
:reset_password_token => nil,
:reset_password_sent_at => nil,
:remember_created_at => nil,
:sign_in_count => 0,
:current_sign_in_at => nil,
:last_sign_in_at => nil,
:current_sign_in_ip => nil,
:last_sign_in_ip => nil,
:created_at => Thu, 12 May 2016 13:48:41 UTC +00:00,
:updated_at => Thu, 12 May 2016 13:48:41 UTC +00:00,
:name => nil,
:surname => nil,
:phone => nil,
:dob => nil,
:gender => nil,
:height => nil,
:about => nil,
:information_source_id => nil,
:institution_id => nil,
:clothes_size_id => nil,
:city_id => nil,
:volunteer_center_id => nil,
:blood_id => nil,
:vkontakte_link => nil,
:medical_contraindications => nil
}
=> nil
最佳答案
How I can fetch the picture owner model here?
class ImageUploader < CarrierWave::Uploader::Base
...
def for_user?(picture)
user = model.imageable
user.is_a? User
# => true
end
...
end
来自
docs :
picture
参数是正在上传的文件对象(这不是您的图片模型的实例)model
是对使用此实例对象的引用 ImageUploader
,在您的情况下是指 Picture
的实例模型。请注意,这与 picture
不同。以上。model
是
Picture
的一个实例,以及
Picture belongs_to :imageable
,以及
:imageable
是一个多态关联,实际上是一个
User
记录您的情况,然后您可以使用我上面的回答访问相关记录。但是请注意,它可能并不总是
User
record 因为它是一个多态关联。
model.imageable
不起作用,因为从嵌套参数设置后可成像记录为零。
class Picture < AbstractModel
belongs_to :imageable, polymorphic: true, inverse_of: :picture # WITHOUT s
更新
inverse_of
不适用于多态关联(
here 和
here )
There are a few limitations to inverse_of support:
- They do not work with :through associations.
- They do not work with :polymorphic associations.
- They do not work with :as associations.
- For belongs_to associations, has_many inverse associations are ignored.
关于ruby-on-rails - Rails 4.2.6 + 多态 + Carrierwave 0.10 + 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37152854/
我正在通过 Carrierwave 上传的裁剪图像的能力。这里是 RailsCast video on Youtube我正在关注的。 但是在上传器中包含 RMagick 后,我收到了: undefin
我正在尝试下载这个 image使用 CarrierWave 但它一直给出此异常: CarrierWave::IntegrityError 我的下载适用于许多图像,但不适用于该特定域。 我看过这个doc
我正在寻找在 Heroku 上的 Rails 应用程序中上传文件的最佳方式。设置:Rails 3、Carrierwave、Heroku、Mongoid 问题:偶尔,当用户上传不同大小的文件时,图像会保
嗨我只是无法找出我的代码有什么问题。我有两个模型 Items 和 Images 以及它们之间的关系 class Item :destroy accepts_nested_attributes_
我实际上是在 Railscast 383 中编写项目- 第二部分,当照片直接上传到 AWS S3 时,然后由 Sidekiq 在后台处理照片以创建照片的缩略图版本。我在 Rails 4 上。 我的问题
我正在尝试使用 Carrierwave 通过 Rails 应用程序将 Logo 图像上传到 Amazon s3 存储桶。但我的文件上传没有将文件读取为 HTTP 文件,并将 NULL 添加到数据库中。
问题是在 mongoids save! 操作中抛出的以下错误。 Mongoid::Errors::Validations: Validation failed - Img failed to be p
我试图在我的 Rails 3.2 应用程序中显示默认图像。我已按照 Carrierwave github 页面上的说明进行操作,并浏览了几篇 Stackoverflow 帖子。但是,出于某种原因,它仍
模型带有图像的远程 url,这意味着它不是在 Rails 中创建的 db 条目。然后第一次从Rails中的DB中获取我想检测图像尚未上传,为图像url分配remote_seed_url并保存!触发 C
Carrierwave 运行良好,我将目录移出公众(出于安全原因),当我尝试上传图像时出现此错误: Errno::EACCES in PostsController#create Permission
我正在使用carrierwave,但遇到了这个问题: 假设一旦项目交付,您需要添加一个部分,其中系统中的图像需要以不同的尺寸显示。我不想为系统中已有的每个图像重新生成新维度。我希望能够在 View 需
是否可以让 CarrierWave 在数据库中存储上传文件的完整路径,而不仅仅是文件名,并在每次访问时重新生成它们? 我希望这样做的原因是能够更改我存储文件的结构,而不会在已上传的文件移动到新位置之前
我正在使用来自主分支和 PostgreSQL 的多个文件上传 我的产品模型有一个名为“images”的字符串字段,我可以很好地附加多个图像。 但是我不知道的是,如何从产品中删除一张图片? 我可以删除文
我有一个允许的文件扩展名列表 def extension_white_list %w(pdf doc docx xls xlsx html tif gif jpg jpeg png bmp rtf
所以,我有一个购物车类,我一直保存在 session 中,直到购买完成,我需要能够在购买完成后将文件上传到购物车(不要问为什么,说来话长),我将所有这些信息转储到一个保存在数据库中的类中。 我经常使用
我正在使用carrierwave 和rmagick 来处理我的图片上传。我现在向 image_uploader.eb 添加了一个新版本(smallthumb): class ImageUploader
我知道我在这里错过了一些非常简单的东西...... CarrierWave::Storage::Fog::File有一个方法 exists ? 我如何使用它?我只是想检查远程存储上是否存在以前上传的文
我正在使用乘客和 capistrano 部署我的第一个 rails 应用程序。 一切都很好,直到我尝试在浏览器中启动应用程序时遇到此错误。 Error message: uninitialize
我有一个使用 CarrierWave gem 的 rails 3 应用程序。到目前为止,我已经上传了 48*48 和 100*100 的图片,但现在我想将它们存储在 200*200 中。 有没有办法调
我有两个模型,每个模型都有自己的 Carrierwave 上传器: class User < ActiveRecord::Base mount_uploader :avatar, AvatarUp
我是一名优秀的程序员,十分优秀!