2011-04-03 14 views
1

我有一個處理2個模型Vehiculo和Poliza的表單。這是我如何讓他們樹立正確的現在:當提交一個帶有fields_for部分的表單時,如何將新的id分配給fields_for模型

class Vehiculo < ActiveRecord::Base 
    has_one :poliza 
end 

class Poliza < ActiveRecord::Base 
    belongs_to :vehiculo 
end 

上Vehiculo的創建方法如下:

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 

上/ VEHICULOS形式/新與來自領域的@fields_for部分poliza。當我提交表格時,它將保存所有字段,但不會將剛剛創建的id從vehiculo分配到polizas表格中的vehiculo_id。在閱讀了關於這個在線的許多問題之後,似乎應該根據模型上的關係「自動地」保存它。這是真的?如果是這樣,爲什麼它不工作?如果沒有,我需要添加到創建方法,所以我解決這個問題?

謝謝!

更新: 使用JSON作爲這裏建議輸出更新創建方法後,我得到什麼:

{ 
    "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" 
    } 
} 

這就是輸出,所以至少它正從表單中的字段,但它是不要將它們插入polizas表。

回答

1

你需要確保對兒童模型,你父模型接受嵌套的屬性:

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

假設您的形式設置正確,你的params會是這個樣子:

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

所以你應該在你的控制器中需要的是:

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 

[更新]

這是你需要有這一切工作。

如上所述,您需要accepts_nested_attributes_for

接下來,確保你的新動作是建立孩子。

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 

最後,在你看來,參考使用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 %> 
+0

我只是說了「accepts_nested_attributes_for」並刪除「&& @ poliza.save」,現在是不是甚至從poliza節省其他領域。我正在使用'<%= fields_for @poliza do | p | %>'爲字段。我只是用poliza的數據來檢查日誌和參數,但是它只是在'插入到Vehiculos'中,沒有插入到Polizas中。任何想法,爲什麼它沒有保存? – capicua 2011-04-03 04:26:49

+0

這是一個很好的問題;我的頭腦並不積極。如果你願意做一些小小的偵探工作,那可能會有所幫助。改變你的'create'方法,使其具有:'render:json => params和return'作爲第一行,並且可以用這個輸出更新你的問題,我們可以從那裏開始。 – 2011-04-03 04:34:24

+0

其實我覺得現在有一些新的行爲和觀點,我想到了。我現在正在更新這個問題。 – 2011-04-03 04:43:27

相關問題