esmanbo
esmanbo

Reputation: 11

How can I make file validation if field does not exist on DB?

Ruby on Rails 4.0

I have item model. Item new form has image field that does not exist on DB. Because I want to save files on directory.

But how can I make validation for file field?

Upvotes: 0

Views: 228

Answers (1)

Sam
Sam

Reputation: 3067

You could add an attr_accessor to your model.

class Item < ActiveRecord::Base
  attr_accessor :file

  validates_presence_of :file
end

Then in the form

<%= f.file_field :file %>

That is one option but I would recommend looking into using Carrier Wave or Paperclip for file uploads. The files are still saved on the file system and are only referenced in the database by their file names.

Upvotes: 2

Related Questions