gpt4 book ai didi

ruby-on-rails - 如何在回形针 ruby​​ on rails 中设置默认图像

转载 作者:数据小太阳 更新时间:2023-10-29 07:11:07 25 4
gpt4 key购买 nike

最近我安装了 Paperclip gem,我正在努力让默认图像在我的系统上工作,我将图像文件放在 assets/images/pic.png 中。

这是我的模型 User 中的代码:

has_attached_file :pic,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => 'missing_:avatar.png'
#:default_url => 'assets/images/avatar.png'

has_attached_file :attach

这是我的 AddPicPaperClip 迁移中的代码:

def self.up
add_column :users, :pic_file_name, :string
add_column :users, :pic_content_type, :string
add_column :users, :pic_file_size, :integer
add_column :users, :pic_updated_at, :datetime
add_attachment :users, :pic
end

def self.down
remove_column :users, :pic_file_name
remove_column :users, :pic_content_type
remove_column :users, :pic_file_size
remove_column :users, :pic_updated_at
remove_attachment :users, :pic
end

为了在我的节目中显示图像,我有以下代码:

<%= image_tag @user.pic.url(:medium) %>

你能帮我找到一个解决方案,以便在用户输入自己的个人资料图片之前显示默认图片吗?

最佳答案

我在 ermenkoff 的解决方案中遇到了错误。

错误:AbsoluteAssetPathError

它已通过从 url 中删除“/assets/”得到修复。

:default_url => ":style/missing_avatar.jpg"

图像存储在:

app/assets/images/medium/missing_avatar.jpg
app/assets/images/thumbs/missing_avatar.jpg

关于ruby-on-rails - 如何在回形针 ruby​​ on rails 中设置默认图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22599504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com