저는 Ruby on Rails에 익숙하지 않아 HABTM 연결 작업을 필사적으로 시도하고 있습니다. 나는 모든 고객이 많은 접촉을 할 수있는 고객 데이터베이스를 가지고있다. 고객의 "쇼"보기에서 선택한 고객에게 새 연락처를 추가하는 링크가 필요합니다.Ruby on Rails 3 : HABTM 연결이 조인 테이블을 업데이트하지 않습니다.
모델/customer.rb :
class Customer < ActiveRecord::Base
has_and_belongs_to_many :contacts
accepts_nested_attributes_for :contacts, :allow_destroy => true
end
모델/contact.rb :
class Contact < ActiveRecord::Base
has_and_belongs_to_many :customers
end
보기/고객/show.html
지금까지, 나는 다음과 같은 데이터가 .erb :
<%= link_to 'Add new contact', new_customer_contact_path(@customer) %>
나 또한 ha 적이 세 개의 테이블 : 내가 클릭 이제
customers (id, name)
contacts (id, name)
contacts_customers (contact_id, customer_id) <- the join-table
-link, 나는 "새로운 고객"페이지로, 그것도 제대로 CUSTOMER_ID를 할당하지만 내가 클릭 할 때 "저장" "새 연락처 추가" 연락처 정보를 채운 후에는 연락처가 만들어 지지만 조인 테이블 (contacts_customers)은 업데이트되지 않습니다. 결과적으로 새로 생성 된 컨택은 고객과 "연결"되지 않습니다.
이미 약 2 시간 동안 인터넷을 탐색 했으므로 도움이되지 않습니다. 누군가가 올바른 방향으로 나를 가리킬 수 있다면 나는 행복 할 것입니다!
감사합니다.
: 여기에 [편집 업데이트 사항] 내가 내 "show.html.erb"보기
Started GET "/customers/260/contacts/new" for 127.0.0.1 at 2012-01-18 08:21:28
Processing by ContactsController#new as HTML
Parameters: {"customer_id"=>"260"}
Customer Load (0.0ms) SELECT 'customers'.* FROM 'customers' WHERE 'customers'.'id' = 260 LIMIT 1
Rendered contacts/_form.html.erb (9.0ms)
Rendered contacts/new.html within layouts/application (13.0ms)
Completed 200 OK in 343 ms (Views: 26.0ms | ActiveRecord: 299.0ms)
에서 "새 연락처"링크를 클릭하면 넘어 갔다받을 매개 변수있어하는 당신은 볼 수 있듯이 customer_id (이 예에서는 260)를 새 연락처 컨트롤러로 전달해야합니다. 나는 그러나 실제로 데이터베이스 항목을 만들 수있는 POST 요청을 시작하는 "저장"을 클릭하면 CUSTOMER_ID 는 손실 만 접촉 테이블이 업데이트됩니다 :
Started POST "/contacts" for 127.0.0.1 at 2012-01-18 08:28:00 +0100
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"jWMlruMezFGOy1j7vTbE9OcL8VareGUXS1AprBpNhGA=", "contact"=>{"anrede"=>"", "name"=>"Test", "position"=>"", "telefon"=>"", "mobil"=>"", "fax"=>"", "email"=>"", "sonstiges"=>""}, "commit"=>"Create Contact"}
BEGIN SQL (0.0ms)
INSERT INTO `contacts` (`anrede`, `email`, `fax`, `mobil`, `name`, `position`, `sonstiges`, `telefon`) VALUES ('', '', '', '', 'Test', '', '', '') (113.0ms)
COMMIT
Redirected to http://localhost:3000/contacts/626
Completed 302 Found in 174ms
나는 그것이의인지 확실하지 않다 어떤 도움이 될지 모르겠지만. 내 경로는 다음과 같이 설정됩니다.
resources :contacts
resources :customers do
resources :contacts
end
답장을 보내 주셔서 감사합니다. 이미 매개 변수 해시를 찾고 내 질문을 업데이트했습니다. 연락처/새 양식과 실제 POST 요청 사이의 어딘가에있는 동안 customer_id가 손실 된 것처럼 보입니다. – Weaz
새 양식에 숨겨진 필드로 customer_id를 추가해야합니다.<% = hidden_field_tag ("customer_id", params [: customer_id] %>를 사용하여 new-POST 요청으로 customer_id 필드를 가져올 수있었습니다. <% = hidden_field_tag ("customer_id", @customer_id) %> – naren
하지만 조인 테이블은 여전히 레일스에 의해 자동으로 업데이트되지는 않습니다. SQL 쿼리를 통해 수동으로 작업하는 것을 고려하고 있습니다.이 작업을 수행 할 수 없기 때문에 :-(나는 내 컨트롤러/모델 설정, 내 손가락을 넣을 수 없습니다. 다른 아이디어가 어쩌면? – Weaz