← Back to Upcase

How to use reject_if with accepts_nested_attributes_for


(Scott Hollinshead) #1

I am trying to stop blank products from being submitted in my nested form.

This is my JobSheet model:

class JobSheet < ActiveRecord::Base
  belongs_to :company
  has_many :products
  accepts_nested_attributes_for :products, reject_if: :reject_products

  def reject_products(attributed)
    attributed['product_code'].blank?
  end
end

This is my new action

def new
  @company = find_company
  @job_sheet = @company.job_sheets.new
  7.times { @job_sheet.products.new }
end

and this is my create action

def create
  @company = find_company
  @job_sheet = @company.job_sheets.new(job_sheet_params)
  @job_sheet.save
  redirect_to root_path
end

The way this is set up at the moment, reject_if: doesn’t seem to be working, have I set this up wrong?

Thanks


(Scott Hollinshead) #2

This is actually working, when I go to the show action, It is only showing the one line that has been filled out.