How to display validation errors in nested form

Hi All,
I have a nested form
OUTER FORM for :doctor

Hi All,
I have a nested form
OUTER FORM for :doctor
INNER FORM for :user

I am having problem is displaying the validation errors for both
doctor and user model. Any idea about how I should be doing this.

Thanks alot in advance.

You can't have a form inside of another one. Maybe I am

I am pretty sure you have have nested forms....:slight_smile:

my nested form looks like

<% form_for :doctor, :url => url do |doc_form| %>

       <td> <%= doc_form.label :"Specialized Field: " %> </td>
       <td> <%= doc_form.text_field :specialize %> </td>
       <td> <%= doc_form.label :"Medical Center Name: " %> </td>
       <td> <%= doc_form.text_field :working_for %> </td>
    <% doc_form.fields_for :user, :url => url do |f| %>

           <td> <%= f.label :"First Name: " %> </td>
           <td> <%= f.text_field :fname %> </td>
I want to display validation errors for both doctor and
create action in the controller looks like

  def create
    doc_attr = params[:doctor]
    user_attr = doc_attr.delete(:user)
    new_doctor =
    new_doctor.user =
    @saved =!

    if @saved == true
      flash[:notice] = "Registration Successful"
      redirect_to login_path()
      render :action => 'new'

Thank you

Instead of having to manually create your child user at the controller-
level, have a look at this:

And the API documentation is at:


you can have fields_for to nest a “form” for a child object

I thought he meant he had 2 form tags, one inside of another.

I think you might be missing something in your models.

In the Doctor model you should have:

  has_many :users
  accepts_nested_attributes_for :users

In the User model you should have:

  belongs_to :doctor

I think I've run into the same problem before and it was due to the
fact that I was missing the 'accepts_nested_attributes_for'.