2012-03-13 33 views
2

當前在我的應用程序中,我有兩個可能的文本框。一個用於本地圖像上傳,另一個用於遠程圖像URL。我確定如果用戶選擇本地圖像文件,則「遠程圖像URL」被清除,反之亦然,因此只有一個框被填充。確定在CarrierWave(RoR)中是否上傳了遠程圖像URL或圖像

我想對他們做一些驗證使用:validate_image_fields。如何確定在該驗證方法中填寫了哪個框?

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 

謝謝!

回答

2

既然你說'只有一個盒子被填滿',你可以確定是否提交了remote_url或是否上傳了本地文件。

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 

這是你在找什麼?我希望我能幫你解決。