Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Partial Image Rendering
    primarykey
    data
    text
    <p>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). I'm having some trouble when attempting to do it via a partial. I've got show.html.erb </p> <pre><code> &lt;fieldset class="fieldset"&gt; &lt;legend&gt;Images&lt;/legend&gt; &lt;%= unless @plant.images.blank? then for image in @plant.images debugger render :partial =&gt; 'show_image', :object =&gt; image end else "This plant has no images to display." end %&gt; &lt;/fieldset&gt; </code></pre> <p>And the partial _show_image.html.erb:</p> <pre><code> &lt;div class="image_container"&gt; &lt;% debugger %&gt; &lt;img src="&lt;%= url_for(:action =&gt; 'picture', :id =&gt; object.id) %&gt;"/&gt; &lt;p class='image_caption'&gt;&lt;%= object.comment %&gt;&lt;/p&gt; &lt;/div&gt; </code></pre> <p>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:</p> <pre><code> &lt;fieldset class="fieldset"&gt; &lt;legend&gt;Images&lt;/legend&gt; #&lt;Image:0x242c910&gt; &lt;/fieldset&gt; </code></pre> <p>When running through the debugger locally:</p> <pre><code> /Users/*****/dev/plantmanager/app/views/plants/show.html.erb:54 debugger (rdb:241) image #&lt;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&gt; (rdb:221) @plant #&lt;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"&gt; (rdb:221) @plant.images [#&lt;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&gt;] (rdb:221) continue /Users/*****/dev/plantmanager/app/views/plants/_show_image.html.erb:2 &lt;% debugger %&gt; (rdb:221) object #&lt;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&gt; (rdb:221) object.id 40 (rdb:221) object.comment "Test" (rdb:221) continue </code></pre> <p>Here are my models [snipped a bit]:</p> <pre><code>class Plant &lt; 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 &lt; ActiveRecord::Base validates_format_of :content_type, :with =&gt; /^image/, :message =&gt; "--- 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload