small medium large xlarge

Generic-user-small
11 Jul 2008, 03:29
Eric Marthinsen (1 post)

Hello-

I’m using the File Upload recipe and running into a problem. My model uses a Product and a ProductImage class. I can create, edit, destroy, and view my Product and ProductImages just fine. The problem is with the validation. When I try to create a new Product with an image that is too large, I get the error messages that I expect. However, when I edit a Product and try to upload an image that is too large, I don’t get the error messages. It almost looks like the error messages are getting wiped out before I try to render the “edit” view again. Here’s the relevent pieces of code:

note: I implemented the relationship between the Product and ProductImage with a has_many relationship instead of has_one. My application code is limiting me to one ProductImage right now, but I’ll relax that restriction in the future.

class ProductsController < ApplicationController
  def update
    @product = Product.find(params[:id])
    @product_image = @product.product_images[0]

    @service = ProductService.new(@product, @product_image)
    
    respond_to do |format|
      if @service.update_attributes(params[:product], params[:product_image_file])
        flash[:notice] = 'Product was successfully updated.'
        format.html { redirect_to(@product) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        #format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  ...

end
class ProductService
  
  attr_reader :product, :product_image
  
  def initialize(product, product_image)
    @product = product
    @product_image = product_image
  end
  
  #this method is currently assuming there is only one product image. If multiple
  #are allowed in the future, then the semmantics of it must change
  def save
    return false unless valid?

    begin
      Product.transaction do
        if(@product_image.new_record?)
          @product.product_images.each {|image| image.destroy} if @product.product_images
          @product.product_images << @product_image
          @product_image.save
        end
        @product.save
        true
      end
    rescue
      false
    end
  end
  
  def update_attributes(product_attributes, product_image_file)
    @product.attributes = product_attributes
    unless product_image_file.blank?
      @product_image = ProductImage.new(:uploaded_data => product_image_file)
    end
    save
  end
  
  def valid?
    @product.valid? && @product_image.valid?
  end
end

this is my edit view

<h1>Editing product</h1>

= error_messages_for :product, :product_image

- form_for(@product, :html => { :multipart => true }) do |f|
  %p
    %b Name
    %br
    = f.text_field :name

  %p
    %b Description
    %br
    = f.text_field :description

  %p
    %b Quantity
    %br
    = f.text_field :quantity

  %p
    %b Price
    %br
    = f.text_field :price

  %p
    %b Product Image
    %br
    = file_field_tag :product_image_file
    
  %p
    = f.submit "Update"

= link_to 'Show', @product
|
= link_to 'Back', products_path

Does anyone know why I’m not getting the error messages? If not, any suggestions for how to debug this?

Thanks in advance- Eric

You must be logged in to comment