【发布时间】:2016-03-13 02:59:22
【问题描述】:
这是我显示图像并发现它们不一致的方式。我分别在模型中创建了两个键:thumb 和 :test。
<% @posts.each do |post|%>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%= image_tag post.user.avatar.url(:thumb), class:"img-thumbnail" %>
<%= image_tag post.user.avatar.url(:test), class:"img-thumbnail" %></br>
<%= post.user.name %>
</h3>
</div>
<div class="panel-body">
<%= post.content %></br>
<%= image_tag post.avatar.url(:medium) %></br>
</div>
</div>
<% end %>
我的用户模型 如您所见,我已经指定了尺寸,但图像并不局限于这些范围。我很困惑为什么要在模型中设置这些尺寸。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, presence: true
has_many :posts
has_attached_file :avatar,
styles: { medium: "300x300>", thumb: "100x100>", test: "64x64>" },
default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
【问题讨论】:
-
您确定问题不是由
img-thumbnailCSS 类引起的吗?假设这是 Bootstrap,该类在图像上设置了max-width: 100%的样式。尝试删除它,或使用浏览器的检查器查看实际图像大小(右键单击图像并在 Chrome 中选择“检查”,它将在工具提示中显示图像的实际和渲染大小)。 -
所以我删除了引导类,并按照你的指示检查了它,它们确实是不同的大小。我上传到网站的图像也不同,但据我了解,图像将调整大小以适合我在模型中分配的参数。
标签: ruby-on-rails ruby-on-rails-4 paperclip