evanx
evanx

Reputation: 1301

Nested form not showing fields

I have a nested form based in a has_one relationship but it's not showing the fields.

What am I missing?

New action

  def new
    @doctor = Doctor.new    
    1.times { @doctor.build_schedule }

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

_form

<%= simple_form_for(@doctor, :html => { class: "form-horizontal"})  do |f| %>

    <%= f.input :name %>

        <%= f.simple_fields_for :schedule do |builder| %>
        <%= render 'days_checkboxes', :f => builder %>

    <%= f.submit %>
<% end %>

Model

class Doctor < ActiveRecord::Base

  has_one :schedule, dependent: :destroy

end

Do I have to set the build in other actions?

Upvotes: 1

Views: 658

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

If there is no schedule associated object on your model instance, yes you will need to call build_schedule wherever you want to reference it. If you didn't do this, the form wouldn't render at all because it has nothing to display the fields for.

Upvotes: 3

Related Questions