简体   繁体   中英

Rails validate_presence message - how to display?

I have the following in the model for Attachments (I'm using Paperclip):

class Attachment < ActiveRecord::Base
  validates_presence_of :name
  validates_presence_of :attach_file_name, :message => "No file selected"

  has_attached_file :attach

Most error messages I get from Rails show up at the top of the screen using this code that's in my layouts:

<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
    <div class="alert alert-<%= name == :notice ? "success" : "error" %>">
      <a class="close" data-dismiss="alert">&#215;</a>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    </div>
<% end %>
<% end %>

Why isn't the validate message showing up?

Thanks for the help!!

UPDATE1

The Attachment gets created from the attachments form _form.html.erb.

This is in the form:

<h4>Attachment:</h4>
<%= f.file_field :attach, :label => 'Attachment' %> 

In order to test the presence of a file from Paperclip, try this:

class Attachment < ActiveRecord::Base
  validates_presence_of :name
  validates_attachment_presence :attach, :message => "No file selected"

  has_attached_file :attach

Also, The Rails 3 -way to do validates is the following:

class Attachment < ActiveRecord::Base
  validates :name, presence: true
  validates :attach, attachment_presence: true, message: 'No file selected!'

  has_attached_file :attach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM