我正在尝试设置一个上传图片页面,用户可以在其中选择上传图片网址。我正在使用carrierwave 视图:
<%= form_for @rating, :html => {:multipart => true} do |f| %>
<p>
<%= f.file_field :pic_url %>
</p>
<p>
<%= f.label :remote_pic_url_url, 'or image url' %>
<br/>
<%= f.text_field :remote_pic_url_url %>
</p>
<div class="actions">
<%= f.submit 'Upload Picture', :class => 'btn btn-primary' %>
</div>
该模型:
class Rating < ActiveRecord::Base
attr_accessible :pic_url, :remote_pic_url_url, :rating
mount_uploader :pic_url , ImageUploader
end
当我尝试仅输入图像 url 时,我收到一条错误消息:
Pic url You are not allowed to upload "" files, allowed types: jpg, jpeg, gif, png
如何使该字段可选。我的印象remote_{columnName}_url
是在carrierwave中添加额外的url字段的约定,这将为我解决这个问题..
控制器代码:
# POST /ratings
# POST /ratings.json
def create
@rating = Rating.new(params[:rating])
respond_to do |format|
if @rating.save
format.html { redirect_to @rating, :notice => 'Rating was successfully created.' }
format.json { render :json => @rating, :status => :created, :location => @rating }
else
format.html { render :action => "new" }
format.json { render :json => @rating.errors, :status => :unprocessable_entity }
end
end
end