2014-08-31 51 views
0

我有以下型號:回報率 - 創造紀錄在許多一對多連接表

Client.rb 
    has_many :establishments 
    accepts_nested_attributes_for :establishments 

    has_many :addressess, through: :establishments 
    accepts_nested_attributes_for :addresses 


Establishment.rb 
    belongs_to :address 
    belongs_to :client 


Address.rb 
    has_many :establishments 

show觀點我爲了創建一個新的地址記錄和新創建的<%= link_to "New Establishment", new_client_address_path(@client)%>Client的建立給客戶。

AddressController我:

 
    def new 
     @client = Client.find(params[:client_id]) 
     @address = @client.addresses.build 
     end

def create 
    @address = Address.new(address_params) 

    respond_to do |format| 
    if @address.save 
     format.html { redirect_to @address, notice: 'Estabelecimento criado com sucesso.' } 
     format.json { render action: 'show', status: :created, location: @address } 
    else 
     format.html { render action: 'new' } 
     format.json { render json: @address.errors, status: :unprocessable_entity } 
    end 
    end 
    end 

這將創建新的地址,但並沒有創造新的建立。這可以自動創建鏈接ClientAddress的新公司嗎?在你問之前,我真的需要有這個Establishment模型。

回答

0

這裏:

在你的模型:

Client.rb 
    has_many :establishments 
    accepts_nested_attributes_for :establishments 

    has_many :addresses, through: :establishments 
    accepts_nested_attributes_for :addresses 


Establishment.rb 
    belongs_to :address 
    belongs_to :client 


Address.rb 
    has_many :establishments 
    has_many :clients, through: :establishments 

在你的控制器:

class AddressController < ApplicationController 

    def new 
    @client = Client.find(params[:client_id]) 
    @client.establishments.build.build_address 
    # if you have a column in establishment, for example: name 
    # you can do something like this to set establishment's name: 
    # @client.establishments.build(name: 'First connection').build_address 
    end 

    # create method doesn't need to be changed!! 

end 

在您的視圖:

<%= form_for(@client) do |form| %> 
    <%= form.input :name %> 

    <%= form.fields_for(:establishments) do |establishment_form| %> 
    <% establishment = establishment_form.object.name.titleize %> 
    <%= establishment_form.input :name, as: :hidden %> 
    <%= establishment_form.fields_for(:address) do |address_form| %> 
     <%= address_form.input :address1, label: "#{establishment} address1" %> 
     <%= address_form.input :address2, label: "#{establishment} address2" %> 
    <% end %> 
    <% end %> 

    <%= form.submit "Submit" %> 
<% end %> 

和你所有的設置! !

+0

您參考爲此地址或客戶的看法? – NunoRibeiro 2014-08-31 23:44:41

+0

我的不好。你已經有客戶,並希望建立和地址被創建?請參閱更新代碼。查看地址/new.html.erb – Surya 2014-09-01 04:09:13

0

首先第一件事情 - 你將無法使用accepts_nested_attributes_for :addresses電話 - 你只能通過立即接通車型

通過關聯數據,我將解釋如何通過你想要的數據,但首先讓我闡述瞭如何正確做到這一點:

-

收集數據

如果你想兩個現有對象通過連接表關聯,你就可以使用一些collection methods in ActiveRecord,即other_ids

#app/views/clients/new.html.erb 
<%= form_for @client do |f| %> 
    <%= f.collection_select :establishment_ids, Establishment.all, :id, :name %> 
    <%= f.submit %> 
<% end %> 

這將填充在Client模型[other]_ids方法,這將基本上填充has_many :through加入你的模型。

如果您有Establishment記錄您已經希望與您新創建的Client聯繫起來,這將僅適用。

-

嵌套屬性

如果你想創建一個新的Establishment記錄,你必須將數據發送到Address模型,然後Establishment模型:

#app/models/client.rb 
class Client < ActiveRecord::Base 
    has_many :establishments 
    has_many :addresses, through: :establishments 
end 

#app/models/establishment.rb 
class Establishment < ActiveRecord::Base 
    belongs_to :client 
    belongs_to :address 
    accepts_nested_attributes_for :address 
end 

#app/models/address.rb 
class Address < ActiveRecord::Base 
    has_many :establishments 
    has_many :clients, through: :establishments 
end 

這將允許您撥打:

#app/controllers/clients_controller.rb 
class ClientsController < ApplicationController 
    def new 
    @client = Client.new 
    @client.establishments.build.build_address 
    end 

    def create 
     @client = Client.new client_params 
     @client.save 
    end 

    private 

    def client_params 
     params.require(:client).permit(establishments_attributes: [address_attributes:[]]) 
    end 
end 

這將允許您使用以下形式:

#app/views/cients/new.html.erb 
<%= form_for @client do |f| %> 
    <%= f.fields_for :establishments do |e| %> 
     <%= e.fields_for :address do |a| %> 
     ... 
     <% end %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 
+0

我想你誤解了我的問題。該表格將在地址控制器中。客戶端將被髮送到AddressController,它將爲客戶端創建一個新的地址。 accepting_nested_attributes_for正在與客戶端和地址一起工作 – NunoRibeiro 2014-08-31 23:49:23