Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1854

Nested Attributes for has_one isn't saving.

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.8
    • Affects Version/s: None
    • Component/s: None
    • None

      I am trying to create a contact which is a has_one relation to a client. I am doing this with nested attributes. I am properly building the contact within the "new" view/controller. When I go to save the contact it is telling me the contact must be present. So for some reason it is not creating the contact.

      ERROR:

      Validation failed - Contact can't be blank.

      Params:

      {
      "utf8"=>"✓",
      "authenticity_token"=>"ep6es356WY5dja7D7C5Kj8Qc0Yvuh3IN2Z1iGG08J7c=",
      "client"=>{
      "contact_attributes"=>

      { "first_name"=>"Scott", "last_name"=>"Baute" }

      ,
      },
      "commit"=>"Create Client"
      }

      Models:

      class Client
      include Mongoid::Document
      include Mongoid::Timestamps

      attr_accessible :role, :contact_id, :contact_attributes

      1. Relationships
        #belongs_to :firm, validate: true
        has_one :contact, validate: true, autosave: true
      1. Matters is custom relationship
        #has_many :client_roles
      1. Nested Attr
        accepts_nested_attributes_for :contact

      validates :contact_id, presence: true

      1. Fields
        field :contact_id
        field :test
        end

      class Contact
      include Mongoid::Document
      include Mongoid::Timestamps
      include Mongoid::MultiParameterAttributes

      #Relationships
      belongs_to :client

      field :first_name
      field :last_name

      end

      Controller:

      1. GET /clients/new
      2. GET /clients/new.json
        def new
        @client = current_firm.clients.new

      respond_to do |format|
      format.html # new.html.erb
      format.json

      { render json: @client }

      end
      end

      1. POST /clients
      2. POST /clients.json
        def create
        @client = current_firm.clients.new(params[:client])

      respond_to do |format|
      if @client.save!
      format.html

      { redirect_to client_path(@client.contact.id), notice: 'Client was successfully created.' }

      format.json

      { render json: @client, status: :created, location: @client }

      else
      format.html

      { render action: "new" }

      format.json

      { render json: @client.errors, status: :unprocessable_entity }

      end
      end
      end

      View:

      • @client.build_contact unless @client_contact
        = semantic_form_for @client, html: { class: "form-horizontal"}

        do |f|
        .control-group
        = render "contact_fields", f: builder
        .form-actions
        = f.submit class: "btn btn-primary"

            Assignee:
            Unassigned Unassigned
            Reporter:
            jwaldrip Jason Waldrip
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: