2014-10-01 2 views
0

나는이 해시으로 반복

"animal_images_attributes"=>{ 
"0"=>{ 
    "image_cache"=>"", 
    "image"=>#<ActionDispatch: : Http: : UploadedFile: [email protected]=#<Tempfile: /tmp/RackMultipart20140930-17710-u3g1hm>, 
    @original_filename="bryony_portfolio.png", 
    @content_type="image/png", 
    @headers="Content-Disposition: form-data; name=\"animal[animal_images_attributes][0][image]\"; filename=\"bryony_portfolio.png\"\r\nContent-Type: image/png\r\n">, 
    "_destroy"=>"false" 
}, 
"1412109521172"=>{ 
    "image_cache"=>"", 
    "image"=>#<ActionDispatch: : Http: : UploadedFile: [email protected]=#<Tempfile: /tmp/RackMultipart20140930-17710-1670i9p>, 
    @original_filename="vandals_portfolio.png", 
    @content_type="image/png", 
    @headers="Content-Disposition: form-data; name=\"animal[animal_images_attributes][1412109521172][image]\"; filename=\"vandals_portfolio.png\"\r\nContent-Type: image/png\r\n">, 
    "_destroy"=>"false" 
    } 
} 
} 

을하고 난 1메가바이트 내가 싶습니다 파일 크기가 끝난 경우, 각 이미지를 반복하고 "이미지"에 대한 파일 크기를 좀하고 싶습니다 그것이 끝났다고 말하는 이미지 (키)에 대한 식별자가 있어야합니다. 나는이 지금까지

class AnimalsController < ApplicationController 
before_action :max_file_size, only: [:create] 

def max_file_size 
image = params[:animal][:animal_images_attributes] 

image.each do |k,v| 
    img_cache = v["image_cache"] 
    img = v["image"] 

    if img 
    tempfilepath = img.tempfile.path 
    file_size = File.size(tempfilepath) 
    if file_size > 1.megabytes 
     @largeImage = true 
    else 
     @largeImage = false 
    end 
    end 
end 
end 

end 

구축하지만 실제로 iteration..Im에있는 모든 이미지가 아닌 사람에 대한 진정한 가치를 할당되어 희망이 내가 할 경우 의미

하게

if file_size > 1.megabytes 
ap(file_size) 
end 

나는이 오른쪽 때문입니다 콘솔

1718186 
1141251 

에 출력 얻을 내 예 : 1MB 이상으로 두 개의 이미지를 추가했습니다.

모든이의 생각은 기본적으로 다음 조건이 클래스의이 걸림돌 약간의 원인으로이 일에 감사

<%= f.fields_for :animal_images do |build| %> 
    <div class="<%= 'large_image' if @largeImage = true %> form-group has-feedback"> 
<% end %> 

어떤 도움을 추가 1메가바이트 이상 각 이미지에 대한 말을하는 것입니다

답변

0

1 메가 바이트를 초과하는 모든 이미지를 반환합니다.

big_images = images.select(&:image).select { |_k, image| File.size(image["image"].tempfile.path) > 1.megabyte } 
+0

내 방법이 무엇입니까? – Richlewis