У меня проблема с тем, что моя simple_form требует как поля загрузки файла изображения, так и ввода URL изображения.
Как проверить, чтобы в обоих случаях требовалось указывать либо поле загрузки файла изображения, либо URL-адрес изображения.
My View в другом контроллере:
<%= f.simple_fields_for :photo_attributes do |d| %>
<%= d.label :image, :label => 'Upload logo' %>
<%= d.file_field :image, :label => 'Image' %>
<%= d.input :image_url, :label => 'Billed URL' %>
<% end %>
Моя фотомодель:
require 'open-uri'
class Photo < ActiveRecord::Base
belongs_to :virksomhed
attr_accessor :image_url
has_attached_file :image,
:url => "/public/images/billeder/photo/:id/:basename.:extension",
:path => ":rails_root/public/images/:id/:basename.:extension"
before_validation :download_remote_image, :if => :image_url_provided?
validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'
private
def image_url_provided?
!self.image_url.blank?
end
def download_remote_image
self.image = do_download_remote_image
self.image_remote_url = image_url
end
def do_download_remote_image
io = open(URI.parse(image_url))
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
end