Search code examples
ruby-on-railsactiverecordfields-for

When submitting a form with a fields_for part, how can I assign the new id to the fields_for model


I have a form that handles 2 models, Vehiculo and Poliza. This is how I have them set up right now:

class Vehiculo < ActiveRecord::Base
  has_one :poliza
end

class Poliza < ActiveRecord::Base
  belongs_to :vehiculo
end

The create method on Vehiculo looks like this:

def create
    @vehiculo = Vehiculo.new(params[:vehiculo])
    @polizadeseguro = Polizadeseguro.new(params[:poliza])

respond_to do |format|
  if @vehiculo.save #&& @poliza.save

    format.html { redirect_to(@vehiculo, :notice => 'Vehiculo was successfully created.') }
    format.xml  { render :xml => @vehiculo, :status => :created, :location => @vehiculo }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @vehiculo.errors, :status => :unprocessable_entity }
  end

end

The form on /vehiculos/new has a @fields_for part with the fields from poliza. When I submit the form, it is saving all the fields, but it is not assigning the just created id from vehiculo, to vehiculo_id on the Polizas table. After reading many questions about this online, It seems that it should save it "automagically" based on the relationships on the model. Is this true? If so, why isn't it working? If not, what do I need to add to the create method so I resolve this?

Thanks!

Update: After updating the create method with json as output as suggested here is what I get:

{
  "utf8"=>"✓",
  "authenticity_token"=>"tEhNC4J17h+KvNgXv1LLkVyufQwU2uAT18P7msQxiqA=",
  "vehiculo"=>{
    "marca_id"=>"2",
    "modelo_id"=>"4",
    "color"=>"Blanco",
    "ano"=>"2011",
    "chassis"=>"123456789",
    "placa"=>"G123456",
    "cliente_id"=>"1",
    "entaller"=>"0",
    "vip"=>"0"
  },
  "poliza"=>{
    "compania"=>"Comp1",
    "numeropoliza"=>"736458",
    "vencimiento(1i)"=>"2011",
    "vencimiento(2i)"=>"9",
    "vencimiento(3i)"=>"21"
  }
}

That's the output, so at least it is getting the fields from the form, but it is not inserting them to the polizas table.


Solution

  • You need to make sure that your parent model accepts nested attributes for the child model:

    class Vehiculo < ActiveRecord::Base
      has_one :poliza
      accepts_nested_attributes_for :poliza
    end
    

    Assuming your form is set up correctly, your params will look something like this:

    params = {
      :vehiculo => {
        :field => "value",
        :another_field => "value",
        :poliza => {
          :poliza_field => "poliza value"
        }
      }
    }
    

    So all you should need in your controller is:

    def create
      @vehiculo = Vehiculo.new(params[:vehiculo])
    
      respond_to do |format|
        if @vehiculo.save #&& @poliza.save
          format.html { redirect_to(@vehiculo, :notice => 'Vehiculo was successfully created.') }
          format.xml  { render :xml => @vehiculo, :status => :created, :location => @vehiculo }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @vehiculo.errors, :status => :unprocessable_entity }
        end
      end
    end
    

    [Update]

    Here's what you'll need to have to have this all work.

    As mentioned above, you need accepts_nested_attributes_for.

    Next, make sure your new action is building the child.

    class VehiculosController < ApplicationController
      def new
        @vehiculo = Vehiculo.new
        @vehiculo.build_poliza
      end
    
      def create
        vehiculo = Vehiculo.new(params[:vehiculo])
        if vehiculo.save
          redirect_to root_path, :notice => "Success"
        else
          redirect_to root_path, :alert => "Failure"
        end
      end
    end
    

    Finally, in your view, reference the child model using fields_for :child_model, as such:

    <%= form_for @vehiculo do |f| %>
      <p>Whatever Field: <%= f.text_field :whatever %></p>
      <%= f.fields_for :poliza do |p| %>
        <p>Polizo Field: <%= p.text_field :something %></p>
      <% end %>
    <% end %>