레일즈 부분 이미지 렌더링
-
08-07-2019 - |
문제
레일에서 속도를 높이는데 이상한 문제가 발생했습니다.데이터베이스에서 일부 이미지를 렌더링하고 있습니다(다른 모델인 식물에 연결된 이미지 모델).부분적으로 수행하려고 할 때 약간의 문제가 있습니다.show.html.erb가 있습니다.
<fieldset class="fieldset">
<legend>Images</legend>
<%= unless @plant.images.blank? then
for image in @plant.images
debugger
render :partial => 'show_image', :object => image
end
else
"This plant has no images to display."
end
%>
</fieldset>
그리고 부분적인 _show_image.html.erb:
<div class="image_container">
<% debugger %>
<img src="<%= url_for(:action => 'picture', :id => object.id) %>"/>
<p class='image_caption'><%= object.comment %></p>
</div>
이것이 렌더링되면 실제 이미지가 아닌 각 이미지에 대해 "#"만 렌더링됩니다.내가 얻은 소스에서와 같이 객체를 문자열로 렌더링하는 것 같습니다.
<fieldset class="fieldset">
<legend>Images</legend>
#<Image:0x242c910>
</fieldset>
로컬에서 디버거를 실행하는 경우:
/Users/*****/dev/plantmanager/app/views/plants/show.html.erb:54
debugger
(rdb:241) image
#<Image id: 40, comment: "Test", name: "Ixia.gif", content_type: "image/gif", data: "GIF89a2\0002\000####$\205\233\tI\250\"x\224\b?\206\031d|ju####\v\031###\247\bI\257G\222\232\222\227\263\262...", plant_id: 55, thumbnail: nil>
(rdb:221) @plant
#<Plant id: 55, name: "Test", description: "Test", created_at: "2009-05-07 07:19:44", updated_at: "2009-05-07 07:19:44", planted: "2009-05-07 00:00:00", sprouted: "2009-05-15 00:00:00", plant_type_id: 1, finished: "2009-05-27 00:00:00">
(rdb:221) @plant.images
[#<Image id: 40, comment: "Test", name: "Ixia.gif", content_type: "image/gif", data: "GIF89a2\0002\000####$\205\233\tI\250\"x\224\b?\206\031d|ju####\v\031###\247\bI\257G\222\232\222\227\263\262...", plant_id: 55, thumbnail: nil>]
(rdb:221) continue
/Users/*****/dev/plantmanager/app/views/plants/_show_image.html.erb:2
<% debugger %>
(rdb:221) object
#<Image id: 40, comment: "Test", name: "Ixia.gif", content_type: "image/gif", data: "GIF89a2\0002\000####$\205\233\tI\250\"x\224\b?\206\031d|ju####\v\031###\247\bI\257G\222\232\222\227\263\262...", plant_id: 55, thumbnail: nil>
(rdb:221) object.id
40
(rdb:221) object.comment
"Test"
(rdb:221) continue
내 모델은 다음과 같습니다.
class Plant < ActiveRecord::Base
has_many :images
validates_presence_of :name
validates_presence_of :plant_type_id
validates_associated :images
after_update :save_images
def image_attributes=(image_attributes)
image_attributes.each do |attributes|
# TODO: Probably handle validation in the image model?
if attributes[:id].blank?
unless attributes[:uploaded_picture].blank?
tmpImg = images.build()
tmpImg.uploaded_picture=attributes[:uploaded_picture]
tmpImg.comment = attributes[:comment]
end
else
img = images.detect { |i| i.id == attributes[:id].to_i }
img.attributes = attributes
end
end
end
def save_images
images.each do |i|
if i.should_destroy?
i.destroy
else
i.save(false)
end
end
end
end
class Image < ActiveRecord::Base
validates_format_of :content_type,
:with => /^image/,
:message => "--- you can only upload pictures"
attr_accessor :should_destroy
def should_destroy?
should_destroy.to_i == 1
end
def uploaded_picture=(picture_field)
self.name = base_part_of(picture_field.original_filename)
self.content_type = picture_field.content_type.chomp
self.data = picture_field.read
#image = MiniMagick::Image.from_blob self.data
#self.thumbnail = resize_and_crop(image, 100).to_blob
end
def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/, '')
end
end
해결책
대신 이것을 시도해 보세요:
<% unless @plant.images.blank?
for image in @plant.images
%>
<%= render :partial => 'show_image', :object => image %>
<% end
else %>
This plant has no images to display.
<% end %>
형식이 아쉽지만 이해가 되실 겁니다.
다른 팁
이미지 모델에서 사진에 대한 메소드 구축
class Image < ActiveRecord::Base
belongs_to :plant
validates_presence_of :uploaded_picture
...
end
식물 컨트롤러에서
def plant_pic
img = Image.find(params[:id])
# THE NEXT LINE IS THE ONE I THINK YOU ARE MISSING:
send_data img.uploaded_picture, :filename =>"plant#{img.plant_id}_#{img.id.to_s}+'.jpg',
:type => 'image/jpeg',
:disposition => 'inline'
end
...
그런 다음 식물 보기에서 다음을 수행합니다.
<fieldset class="fieldset">
<legend>Images</legend>
<% if plant.images.blank? -%>
<p>This plant has no images to display.</p>
<% else %>
<%= @plant.images.each do |image| %>
<div class="img_container">
<img src="<%= url_for(:action => 'plant_pic', :id => image.id) %>"/>
</div>
<% end -%>
<% end -%>
</fieldset>
이미지 컨트롤러에서 Upload_picture 바이너리 필드를 어떻게든 인코딩/디코딩하고 싶을 수도 있지만(저는 Base64.encode 및 Base64.decode를 사용합니다) 이는 또 다른 문제입니다.
도움이 되었기를 바랍니다.
제휴하지 않습니다 StackOverflow