1
다른 테이블에서 ID를 저장하기 위해 양식보기를 만들었지 만 NULL을 저장하고 작동하지 않습니다. 여기관계에있는 다른 테이블의 ID를 저장할 수 없습니다.
내 테이블 : 여기
class PurchaseProductController < ApplicationController
def new_product
@obj_product = PurchaseProduct.new(params[:obj_product])
@obj_supplier_product = SupplierProduct.new(params[:obj_supplier_product])
end
def create_product
@obj_product = PurchaseProduct.new(params[:obj_product])
@obj_supplier_product = SupplierProduct.new(params[:obj_supplier_product])
if @obj_product.save()
@obj_supplier_product.save()
end
end
end
내 모델 : 여기
|purchase_products|
|id| |name| #A.I (AUTO INCREMENT IN 1)
A.I varchar(255)
1 ABC
2 DEF
|supplier_products|
|id| |purchase_product| |amount|
A.I integer integer
1 2 1000
2 1 2000
는 컨트롤러 여기
class PurchaseProduct < ActiveRecord::Base
has_many :supplier_products
end
class SupplierProduct < ActiveRecord::Base
belongs_to :purchase_product
end
가 내보기
<% form_for :obj_product, :url => {:controller=>"purchase_product",:action=>'create_product'} do |f|%>
Amount:<%= text_field_tag 'obj_product[name]',@name %>
ID <%= text_field_tag 'obj_supplier_product[id]',@obj_product.id %>
<%= submit_tag "Create",:class=>"button" %>
<% end %>
012 purchase_products에서 ID가 아직도 내가 노력 supplier_products
에 저장되지 않고
Processing PurchaseProductController#create_product (for 127.0.0.1 at 2014-07-23 15:56:48) [POST]
Parameters: {"obj_supplier_product"=>{"purchase_product_id"=>"", "amount"=>"1800", "supplier_id"=>"6"}, "obj_product"=>{"name"=>"Impresora "}, "authenticity_token"=>"yeah", "commit"=>"Create"}
PurchaseProduct Create (0.2ms) INSERT INTO `purchase_products` (`name`) VALUES('Printer')
SQL (29.9ms) COMMIT
SQL (0.1ms) BEGIN
SupplierProduct Create (0.0ms) Mysql::Error: Column 'purchase_product_id' cannot be null:
INSERT INTO `supplier_products` (`purchase_product_id`, `amount`) VALUES(NULL, 1800.0)
작동하지 :
def create_product
@obj_product = PurchaseProduct.new(params[:obj_product])
@obj_supplier_product = SupplierProduct.new(params[:obj_supplier_product])
if @obj_product.save()
@obj_product.id= @obj_supplier_product.purchase_product_id
@obj_supplier_product.save()
end
end
제발 누군가가 나를 도울 수 3,516,여기
로그입니까?
감사합니다 +1, 나를 위해 일했다,하지만 난 것으로 나타났습니다 나는 대답 =에 가까웠다.) 그러나 어쨌든 단지 정신적 인 것 뿐이었다 .... 고마워요, 아저씨 =) –