0

New to rails and I'm trying to reject an empty nested file_field but it just keeps going though. Here is the setup.

EDIT: It does actually save the image properly if one is included, just doesn't reject the empty ones.

Fihish Model

class Finish < ActiveRecord::Base
  default_scope order('finishes.id ASC')
  attr_accessible               :name, 
                                :title,

                                ##belongs_to##
                                :sku_id,

                                ##has_many##
                                :image_attributes


  belongs_to                    :skus

  has_one                       :image, as: :imageable, :dependent => :destroy
  accepts_nested_attributes_for :image, :reject_if => lambda { |a| a[:asset].blank? }, :allow_destroy => true

  validates_presence_of         :title

  before_save                   :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

Finishes Controller

def new
  @finish = Finish.new

  @finish.build_image

  respond_to do |format|
    format.html # new.html.erb
    # format.json { render json: @finish }
  end
end

Image Model

class Image < ActiveRecord::Base
  attr_accessible               :content, #remove if no longer necessary
                                :asset

  belongs_to                    :imageable, polymorphic: true

  mount_uploader                :asset, ImageUploader
end

Form

= form_for @finish, :html => { :multipart => true } do |f|
  - if @finish.errors.any?
    #error_explanation
      %h1= "#{pluralize(@finish.errors.count, "error")} prohibited this finish from being saved:"
      %ul
        - @finish.errors.full_messages.each do |msg|
          %li= msg

  %fieldset{id: "finishes"}
    .field
      = f.label :title
      = f.text_field :title

    #finish-image.images
      = f.fields_for :image do |image_builder|
        = render 'images/image_fields', f: image_builder

  .actions
    = f.submit 'Save'

image_fields partial

.field
  = f.label :asset, "Image"
  = f.file_field :asset

- if f.object.asset
  .image-box
    = image_tag f.object.asset_url(:thumb).to_s


.remove-fields
  = link_to_remove_fields f
Eric Norcross
  • 4,177
  • 4
  • 28
  • 53
  • Can you replace the block of code `a[:asset].blank?` with `raiser a` and tell us the output? – MrYoshiji May 28 '13 at 20:20
  • I get: undefined method `raiser' for # – Eric Norcross May 28 '13 at 20:32
  • I think MrYoshiji meant 'raise'. Take a look at the params that are coming in (either in logs, or by calling raise in the controller or lambda procedure). There may be a clue in there that indicates why a[:asset] isn't 'blank'. – Mark Swardstrom May 28 '13 at 22:06
  • Here is the output from the logs: `SQL (0.5ms) INSERT INTO "finishes" ("created_at", "name", "product_id", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Wed, 29 May 2013 01:06:30 UTC +00:00], ["name", "aslkdjf"], ["product_id", nil], ["title", "aslkdjf"], ["updated_at", Wed, 29 May 2013 01:06:30 UTC +00:00]]` – Eric Norcross May 29 '13 at 01:07
  • I don't see anything in there. Perhaps I'm confused about what this is actually doing. My assumption was that if the model received an empty nested attribute it would kick back to the form and give an error message (the same way it would work with a validation). – Eric Norcross May 29 '13 at 01:09
  • This is what I was looking for: http://stackoverflow.com/questions/5144527/nested-models-and-parent-validation – Eric Norcross May 29 '13 at 01:24
  • More accurately, this worked exactly how I wanted it to: https://github.com/carrierwaveuploader/carrierwave/issues/670#issuecomment-16343022 – Eric Norcross May 29 '13 at 01:48

0 Answers0