嗨,我在控制器操作中给出了这个:
def create
@department = Department.create(department_params)
if @department.attachment.attach_content_type == 'image\/.*'
render :action => :new
else
redirect_to departments_path, notice: I18n.t('department_created')
end
end
我在我的参数中得到了这个
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y2GmRyk6SEh12vJQMEWpdphojFeCXWxhECLaJ1bBww6sZ7P9CmjLEmxK/vGPx2tZ+15zy0W4+tYf26zpHSL9Yw==", "department"=>{"name"=>"Quality Assurancebnnnnnnnnnnnn", "attachment_attributes"=>{"attach"=>#<ActionDispatch::Http::UploadedFile:0xb41fac20 @tempfile=#<Tempfile:/tmp/RackMultipart20150608-4331-a3x9h7.jpeg>, @original_filename="index.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"department[attachment_attributes][attach]\"; filename=\"index.jpeg\"\r\nContent-Type: image/jpeg\r\n">}}, "button"=>""}
它不会进入我的if条件,而是在我的其他部分,但是content_type是image / jpeg,我想如果我的content_type从图像开始那么它将无法保存。我也试过这个
p "*********************************8"
p (@department.attachment.attach_content_type).to_s
@content_type = (@department.attachment.attach_content_type).to_s
p 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr'
p @content_type == "image\/.*"
但它在控制台中给出了这个输出:
"*********************************8"
"image/jpeg"
"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
false
请指导如何解决此问题。
答案 0 :(得分:2)
您已将正则表达式定义为字符串:'image\/.*'
尝试改为:
if @department.attachment.attach_content_type.to_s =~ /image\/.*/