在 ruby rails 上通过回形针根据用户名的第一个字符添加缺失的图像
Add missing images based on user name's first character through paper clip in ruby on rails
我使用回形针gem上传和显示图片。
我想根据用户名字的第一个字符显示丢失的图像。假设用户名是 "Foo",丢失的图像上应该显示字母 F。
同样,如果用户的名字是 "Bar",丢失的图像应该是带有字母 "B" 的图像。通用缺失图像工作正常。我有所有 26 张图片,上面有 26 个单独的字母。谁能帮忙。
这是我在用户模型中的代码。
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100", small: "50x50", mini: "25x25>", xsmall: "15x15" },
default_url: ":style/missingdp.jpeg",
url: "/assets/users/:id/:style/:basename.:extension",
path: ":rails_root/public/assets/users/:id/:style/:basename.:extension"
validates_attachment_content_type :avatar, { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] }
validates_attachment_size :avatar, less_than: 3.megabytes
您需要一种方法来查看它们是否有缩略图,如果没有,则为它们选择合适的缺失缩略图。按照这些思路,您可以调整它以使用适当的路径和方法名称。
class User < ActiveRecord::Base
def avatar_or_missing_path
if self.avatar
self.avatar.public_path #or whatever the method is to get the path to the avatar image
else
"/images/avatars/missing/#{self.first_name[0].upcase}.jpg"
end
end
end
然后在视图中你可以说
<%= image_tag user.avatar_or_missing_path %>
或其他什么。
based on the first character of the user's first name
一种更有效的方法是将 CSS 与 :before
伪 class 一起使用,并填充 attr
function (JSFiddle
):
#app/views/users/show.html.erb
<%= content_tag :div, class: "user_image", data: { name: @user.name[0] } do %>
<%= image_tag ... if @user.avatar %>
<% end %>
#app/assets/stylesheets/application.css
.user_image {
display: block;
position: relative;
width: 50px;
height: 50px;
margin: 1px;
background: #f00;
}
.user_image:before {
content: attr(data-name);
color: #fff;
position: absolute;
font-size: 18px;
left: 40%;
top: 30%;
}
这意味着您应该从 User
头像中删除 missing
功能:
#app/models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100", small: "50x50", mini: "25x25>", xsmall: "15x15" },
url: "/assets/users/:id/:style/:basename.:extension",
path: ":rails_root/public/assets/users/:id/:style/:basename.:extension"
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] }, size: { less_than: 3.megabytes }
end
插值
如果你想让 "missing" 图像使用用户名的第一个字母,你需要查看 Paperclip Interpolation
:
#app/models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, default_url: ":style/missing/:missing_user.jpeg"
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] }, size: { less_than: 3.megabytes }
end
#config/initializers/paperclip.rb
Paperclip.interpolates :missing_user do |attachment, style|
attachment.instance.user.name[0] #-> might be attachment.instance.name[0]
end
最后,如果你想清理回形针模型,你应该查看 default_options
散列(可以在模型中覆盖):
#config/application.rb
config.paperclip.defaults = {
styles: { medium: "300x300", thumb: "100x100", small: "50x50", mini: "25x25>", xsmall: "15x15" },
url: "/assets/users/:id/:style/:basename.:extension",
path: ":rails_root/public/assets/users/:id/:style/:basename.:extension"
}
#app/models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar
end
我自己的生成带有用户首字母的头像的解决方案:https://github.com/igorkasyanchuk/avatarro
<%= image_tag Avatarro.image(current_user.full_name), size: '24x24' %>
<%= image_tag Avatarro.image("Igor Kasyanchuk", solid: true), size: '24x24' %>
<%= image_tag Avatarro.image("IK"), size: '32x32' %>
<%= raw Avatarro.svg("IK") %>
我使用回形针gem上传和显示图片。
我想根据用户名字的第一个字符显示丢失的图像。假设用户名是 "Foo",丢失的图像上应该显示字母 F。
同样,如果用户的名字是 "Bar",丢失的图像应该是带有字母 "B" 的图像。通用缺失图像工作正常。我有所有 26 张图片,上面有 26 个单独的字母。谁能帮忙。 这是我在用户模型中的代码。
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100", small: "50x50", mini: "25x25>", xsmall: "15x15" },
default_url: ":style/missingdp.jpeg",
url: "/assets/users/:id/:style/:basename.:extension",
path: ":rails_root/public/assets/users/:id/:style/:basename.:extension"
validates_attachment_content_type :avatar, { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] }
validates_attachment_size :avatar, less_than: 3.megabytes
您需要一种方法来查看它们是否有缩略图,如果没有,则为它们选择合适的缺失缩略图。按照这些思路,您可以调整它以使用适当的路径和方法名称。
class User < ActiveRecord::Base
def avatar_or_missing_path
if self.avatar
self.avatar.public_path #or whatever the method is to get the path to the avatar image
else
"/images/avatars/missing/#{self.first_name[0].upcase}.jpg"
end
end
end
然后在视图中你可以说
<%= image_tag user.avatar_or_missing_path %>
或其他什么。
based on the first character of the user's first name
一种更有效的方法是将 CSS 与 :before
伪 class 一起使用,并填充 attr
function (JSFiddle
):
#app/views/users/show.html.erb
<%= content_tag :div, class: "user_image", data: { name: @user.name[0] } do %>
<%= image_tag ... if @user.avatar %>
<% end %>
#app/assets/stylesheets/application.css
.user_image {
display: block;
position: relative;
width: 50px;
height: 50px;
margin: 1px;
background: #f00;
}
.user_image:before {
content: attr(data-name);
color: #fff;
position: absolute;
font-size: 18px;
left: 40%;
top: 30%;
}
这意味着您应该从 User
头像中删除 missing
功能:
#app/models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100", small: "50x50", mini: "25x25>", xsmall: "15x15" },
url: "/assets/users/:id/:style/:basename.:extension",
path: ":rails_root/public/assets/users/:id/:style/:basename.:extension"
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] }, size: { less_than: 3.megabytes }
end
插值
如果你想让 "missing" 图像使用用户名的第一个字母,你需要查看 Paperclip Interpolation
:
#app/models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, default_url: ":style/missing/:missing_user.jpeg"
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] }, size: { less_than: 3.megabytes }
end
#config/initializers/paperclip.rb
Paperclip.interpolates :missing_user do |attachment, style|
attachment.instance.user.name[0] #-> might be attachment.instance.name[0]
end
最后,如果你想清理回形针模型,你应该查看 default_options
散列(可以在模型中覆盖):
#config/application.rb
config.paperclip.defaults = {
styles: { medium: "300x300", thumb: "100x100", small: "50x50", mini: "25x25>", xsmall: "15x15" },
url: "/assets/users/:id/:style/:basename.:extension",
path: ":rails_root/public/assets/users/:id/:style/:basename.:extension"
}
#app/models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar
end
我自己的生成带有用户首字母的头像的解决方案:https://github.com/igorkasyanchuk/avatarro
<%= image_tag Avatarro.image(current_user.full_name), size: '24x24' %>
<%= image_tag Avatarro.image("Igor Kasyanchuk", solid: true), size: '24x24' %>
<%= image_tag Avatarro.image("IK"), size: '32x32' %>
<%= raw Avatarro.svg("IK") %>