2015-06-08 45 views
0

嗨,我在我的控制器行動給出這樣的:比賽中的regex控制器條件rails4

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 

和我得到這個在我的PARAMS

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"=>""} 

它不是我的,如果條件裏想它去在我的其他部分,但content_type是圖像/ 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 

請指導如何解決這個問題。

回答

2

您已經定義了正則表達式作爲一個字符串:'image\/.*'

嘗試,而不是:

if @department.attachment.attach_content_type.to_s =~ /image\/.*/ 
+0

沒有它不工作 – Aniee

+0

是'@ department.attachment.attach_content_type'實際上是一個字符串?看到更新,強制它與'to_s' – arco444

+0

是的,它是字符串 – Aniee