EverTheLearner
EverTheLearner

Reputation: 7200

Determine if a remote image URL or an image was uploaded in CarrierWave (RoR)

Currently in my application I have two possible textboxes. One for a local image upload, and another for a remote image URL. I am making sure that if the user selects a local image file then the "Remote Image URL" gets cleared, and vice versa, so only one box is filled.

I'm trying to do some validation on them using :validate_image_fields. How do I determine which box is filled out in that validation method?

new.html.erb

<%= form_for @painting, :html => {:multipart => true} do |f| %>
    <p>
      <%= f.file_field :image %>
    </p>
    <p>
      <%= f.label :remote_image_url, "or image URL" %><br />
      <%= f.text_field :remote_image_url %>
    </p>
  <p><%= f.submit %></p>
<% end %>

gallery.rb

class List < ActiveRecord::Base
  attr_accessible :description, :image, :remote_image_url

  validate :validate_image_fields

  def validate_minimum_image_size
    # how do i determine if it is a remote image or local image?
  end

Thank you!

Upvotes: 2

Views: 946

Answers (1)

prasvin
prasvin

Reputation: 3009

Since you say that 'only one box is filled', you could identify if a remote_url is submitted or a local file is uploaded.

def validate_image_fields
  if remote_image_url.present?
     # condition/validations for the image url
  elsif image
     # condition/validations for uploaded image
  else
     errors.add(:base, "No image url or local image provided")
  end
end

Is this what you were looking for? I hope I got you correct.

Upvotes: 2

Related Questions