Rails API not receiving cURL POST with nested json correctly
<p>I have a Rails 5 api which should be able to create Visit objects from JSON sent in HTTP POST requests and store that in the database.
The Visit model has an association (has_one) with the VisitComplement model.
The post request that I'm sending with cURL also has fields for this association.</p>

<p>The problem is that the VisitComplement data is not being stored in the database, only the Visit data is being stored.</p>

<p><strong>Visit model:</strong></p>

<pre><code>class Visit &lt; ApplicationRecord
  has_one :visit_complement
  has_one :physical_exam

  accepts_nested_attributes_for :visit_complement

end
</code></pre>

<p><strong>VisitsController:</strong></p>

<pre><code>class VisitsController &lt; ApplicationController

  def create
    @visit = Visit.new(visit_params)

    if @visit.save
      render json: @visit, status: :created, location: @visit
    else
      render json: @visit.errors, status: :unprocessable_entity
    end
  end

  private
    def visit_params
      params.require(:visit).permit(:fumo, :alcool, visit_complement_attributes: [:sintomas])
    end
end
</code></pre>

<p><strong>The cURL POST request:</strong></p>

<pre><code>curl -H "Accept: application/json" -H "Content-type: application/json" -d '{"fumo": "10", "visit_complement_attributes": {"sintomas": "febre"}}'  http://localhost:3000/api/v1/visits
</code></pre>

<p>When I run the above command I get the following output from the server:</p>

<pre><code>&lt;!-- language: lang-none --&gt;
Started POST "/api/v1/visits" for ::1 at 2016-11-13 16:14:56 -0200
Processing by VisitsController#create as JSON
  Parameters: {"fumo"=&gt;"10", "visit_complement_attributes"=&gt;{"sintomas"=&gt;"febre"}, "visit"=&gt;{"fumo"=&gt;"10"}}
Can't verify CSRF token authenticity.
   (0.2ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "visits" ("fumo", "created_at", "updated_at") VALUES (?, ?, ?)  [["fumo", 10], ["created_at", 2016-11-13 18:14:56 UTC], ["updated_at", 2016-11-13 18:14:56 UTC]]
   (1.0ms)  commit transaction
Completed 201 Created in 33ms (Views: 3.0ms | ActiveRecord: 2.4ms)
</code></pre>

<p><strong>Aditional Info</strong></p>

<ol>
<li>Although it's a rails API, I didn't create it with the --api option.</li>
<li>I'm using the rack-cors gem to bypass the CSRF authentication problem</li>
<li>I don't have a controller for the VisitComplement model.</li>
</ol>