Validate behaving strangely in a nested form.

27 views
Skip to first unread message

Luis

unread,
May 14, 2016, 7:39:18 PM5/14/16
to Ruby on Rails: Talk
Hi all,

this form will not display the nested part when it fails validation.
What is really throwing me in a loop is that if eliminate one of the ":include_blank => true"
it works.

I have tried changing them to text_field and it fails also.

Thanks for you help in advance.
-Luis

class Elig < ActiveRecord::Base
  belongs_to :patient

  validates_presence_of :contract, :effective
end

class Patient < ActiveRecord::Base
     has_one :elig

    validates_presence_of :dob, :name, :last
    validates :name, uniqueness: {scope: [:name, :last, :dob]}
    validates_associated :elig
end

= form_for(@patient) do |f|
  - if @patient.errors.any?
      %h2
        = pluralize(@patient.errors.count, "error")
        prohibited this patient from being saved:
      %ul
        - @patient.errors.full_messages.each do |msg|
          %li= msg
  .field
    =f.label :Name
    = f.text_field :name
    /more text_field/
    /---------------/
    %br
    =f.fields_for :elig do |e|
      %br
      = e.label :Efective
      = e.date_select :effective, start_year: Date.today.year,order: [:day,:month, :year], :include_blank => true
      %br
      = e.label       :Termination
      = e.date_select :termination, start_year: Date.today.year,order: [:day,:month, :year], :include_blank => true
      %br
      = e.label :Renewal
      = e.date_select :renewal, start_year: Date.today.year,order: [:day,:month, :year], :include_blank => true
      %br
      = e.label       :Contract
      = e.text_field :contract
      %br
  .actions
    = f.submit

Luis

unread,
May 14, 2016, 10:54:13 PM5/14/16
to Ruby on Rails: Talk
I forgot to post this line for the Patient class.
    accepts_nested_attributes_for :elig, reject_if: :all_blank, allow_destroy: false

Luis

unread,
May 15, 2016, 12:53:11 PM5/15/16
to Ruby on Rails: Talk
I fix the problem.
It is very annoying when this types of mistakes do not generate an error. 
(It is almost as bad as my day of c programming chasing a memory leak.
 dgb to the rescue.)

class PatientsController < ApplicationController
def create
   
@patient = Patient.new(patient_params)
   
@elig =@patient.build_elig  patient_params["elig_attributes"] #***** I was missing this line
   
debugger
    respond_to
do |format|
     
if @patient.save
        format
.html { redirect_to @patient, notice: 'Patient was successfully created.' }
        format
.json { render :show, status: :created, location: @patient }
     
else
        format
.html { render :new }
        format
.json { render json: @patient.errors, status: :unprocessable_entity }
     
end
   
end
 
end


Chris Vukin

unread,
May 21, 2016, 10:50:14 AM5/21/16
to Ruby on Rails: Talk
this looks like a very interesting project, I've been having some issues with assigning associations thru a form on an related type of app, getting a form that crates an polymorphic association when a provider creates an order and such. would you be up for an email chat on the topic by chance? I've a demo application up here: https://warm-river-76092.herokuapp.com thanks for posting the solution to your issue!
Reply all
Reply to author
Forward
0 new messages