简体   繁体   English

Rails部分图像渲染

[英]Rails Partial Image Rendering

I'm getting up to speed on rails and ran into an odd problem. 我要加快速度,遇到一个奇怪的问题。 I'm rendering some images from the database (Image models attached to another model, Plants). 我正在从数据库渲染一些图像(将图像模型附加到另一个模型Plants)。 I'm having some trouble when attempting to do it via a partial. 尝试通过部分操作时遇到麻烦。 I've got show.html.erb 我有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>

And the partial _show_image.html.erb: 还有部分_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>

When this is rendered it just renders a "#" for each image, rather than the actual image. 呈现此效果时,它只会为每个图像而不是实际图像呈现一个“#”。 It seems to be just rendering the object as a string, as in the source I get: 似乎只是将对象呈现为字符串,如在我得到的源中:

 <fieldset class="fieldset">
      <legend>Images</legend>
      #<Image:0x242c910>
 </fieldset>

When running through the debugger locally: 在本地运行调试器时:

 /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

Here are my models [snipped a bit]: 这是我的模型[略过]:

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 

Try this instead: 尝试以下方法:

<%  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 %>

Sorry about the formatting, but you get the idea. 抱歉格式化,但是您知道了。

Build a method for the pic in your Image model 为您的图片模型中的图片建立方法

class Image < ActiveRecord::Base
   belongs_to :plant
   validates_presence_of :uploaded_picture
   ...
end

In your plants controller 在您的工厂控制器中

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
...

And then in your plants view: 然后在您的工厂视图中:

<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>

You may want to encode/decode the uploaded_picture binary field someway (I use Base64.encode and Base64.decode) in your images controller, but that's another issue. 您可能希望在图像控制器中以某种方式编码/解码upload_picture二进制字段(我使用Base64.encode和Base64.decode),但这是另一个问题。

Hope that helps you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM