简体   繁体   English

提交带有fields_for部分的表单时,如何将新的id分配给fields_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. 我有一个处理2种模型的表格,即Vehiculo和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: Vehiculo上的create方法如下所示:

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. / vehiculos / new上的表单具有@fields_for部分,其中包含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. 当我提交表单时,它会保存所有字段,但不会将刚刚创建的id从vehiculo分配给Polizas表上的vehiculo_id。 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? 如果不是,我需要在create方法中添加什么才能解决此问题?

Thanks! 谢谢!

Update: After updating the create method with json as output as suggested here is what I get: 更新:按照建议的那样将json作为输出更新create方法后,这就是我得到的:

{
  "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. 这就是输出,因此至少它是从表单中获取字段,但没有将其插入到polizas表中。

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将如下所示:

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 . 如上所述,您需要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: 最后,在您看来,使用fields_for :child_model引用子模型,如下所示:

<%= 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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM