我使用carrierwave上传用户模型中的 Profiles 图片 . 如果用户尝试上载任何非图像的文件,则必须引发错误 . 但是,错误会在屏幕上显示两次 . 请帮忙
code for user model class User <ActiveRecord :: Base
include CarrierWave::MiniMagick
validates :email, :presence =>true, :uniqueness => {case_sensitive: false}, :format => { :with=> /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/, :message => "please enter a valid e-mail" }
validates :name, :presence=>true
validates :password ,:presence =>true, :confirmation=> true #, :length =>{ :minimum=>6, :maximum=>30}, :format=>{:with=>/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,30}/}
#for the image
mount_uploader :image, ImageUploader
#for the password
has_secure_password
结束
**代码ImageUploader **
def scale(width, height)
image.resize widthxheight
end
#Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [50, 50]
end
# Add a white list of extensions which are allowed to be uploaded.
def extension_white_list
%w(jpg jpeg gif png)
end
错误部分的代码
<% if object.errors.any?%>
<ul>
<%= object.errors.full_messages.each do |message|%>
<li><%= message%></li>
<%end%>
</ul>
<%end%>
2 回答
在
erb
中,<% .. %>
用于评估其中的Ruby代码,<%= .. %>
用于评估以及在erb中打印输出 .在您的下面的代码中,您使用了
<%= ... %>
两次,一次显示错误消息<%= message%>
和其他使用<%= object.errors.full_messages.each do |message|%>
显示相同的错误消息 . 这导致错误消息显示两次 . 修改您的代码如下所示,您只需要<%= ... %>
,而在迭代错误消息集合时不显示错误消息 .更改
至
注意
<%
在开头而不是<%=
<%=
用于打印出已计算表达式的输出 . 使用<%
用于指示仅应评估表达式并且不打印输出 .