本文介绍了错误在滑轨中显示两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用载波在用户模型中上传个人资料图片。
如果用户尝试上传不是图像的任何文件,则必须引发错误。但是,错误在屏幕上显示两次。
请帮助

I am using carrierwave to upload the profile picture in user model.If the user tries to upload any file that is not an image, then an error must be raised. However the error is displayed twice on the screen.Please help

用户模型代码
类User< ActiveRecord :: Base

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

end

**代码ImageUploader **

**code 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%>


推荐答案

erb中<%..%> 用于评估其中的Ruby代码,而<%= ..% > 用于评估并在erb中打印输出。

In an erb, <% .. %> is used to evaluate the Ruby code within it and <%= .. %> is used to evaluate as well as print the output in erb.

在下面的代码中,您使用了 <%= ...%> 两次,一次以<%= message%> 显示错误消息,其他以使用<%= object.errors.full_messages.each | message |%> 显示相同的错误消息。
这将导致错误消息显示两次。如下修改代码,只需要<%= ...%> 即可显示错误消息,而无需遍历错误消息的集合。

In your below code you have used <%= ... %> twice, once to display the error message with <%= message%> and other to display the same error messages using <%= object.errors.full_messages.each do |message|%> .This is resulting in error messages being displayed twice. Modify your code as below, you just need <%= ... %> while displaying error message not while iterating over the collection of error messages.

<% object.errors.full_messages.each do |message|%> <%# Removed "=" %>
  <li><%= message%></li>
<%end%>

这篇关于错误在滑轨中显示两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:02