我有以下型號:回報率 - 創造紀錄在許多一對多連接表
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 enddef 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
這將創建新的地址,但並沒有創造新的建立。這可以自動創建鏈接Client
和Address
的新公司嗎?在你問之前,我真的需要有這個Establishment
模型。
您參考爲此地址或客戶的看法? – NunoRibeiro 2014-08-31 23:44:41
我的不好。你已經有客戶,並希望建立和地址被創建?請參閱更新代碼。查看地址/new.html.erb – Surya 2014-09-01 04:09:13