dimanche 10 mai 2015

How to make a file_field required before submit in erb Rails

I'm trying to make a file_field required before the form is able to be submitted, using Rails and ERB, and I'm drawing a blank.

<%= form_for Image.new, html: {multipart: true} do |i| %>
  <%= i.label :description %>
  <%= i.text_field :description %>
  <%= i.file_field :image %>  <------**** This Line ****
  <%= i.submit "Upload" %>
<% end %>

I've tried using 'required' in various ways and searched the web but to no avail. Is this something that I should seek to validate in the model instead?

My model is this:

class Image < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_many :likes, dependent: :destroy
  belongs_to :user

  has_attached_file :image, :styles => { :large => "600x600>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

Aucun commentaire:

Enregistrer un commentaire