RoR을 배우고 accepts_nested_attributes_for 및 has_and_belongs_to_many를 사용하여 전통적으로 두 가지 형식 인 정보를 제출하려고합니다. 호환되는 일부 사이트, 호환되지 않는 사이트 및 일부 사이트를 모르는 경우를 읽었습니다. 참고로 레일즈 2.3.4를 사용하고 있습니다. Ryan 's Scraps 튜토리얼에서 모델링 해 보았습니다 nested modelsaccepts_nested_attributes_for 및 has_and_belongs_to_many를 사용하려고 시도했지만 join 테이블이 채워지지 않습니다.
디버깅을 시도한 결과 두 가지 문제점이있는 것으로 보이지만 그 이유는 확실하지 않습니다.
- 중첩 모델이 포함 된 양식을 제출하면 중첩 모델 정보의 일부만 게시됩니다. 사용자가 선택할 수있는 "n"이 아닌 첫 번째 필드 만 가져옵니다.
- 게시되는 단일 필드 중에서 HABTM 관계에 대해 만든 조인 테이블에 삽입 된 행이 없습니다.
변호사 모델 :
class Attorney < ActiveRecord::Base has_and_belongs_to_many :associations accepts_nested_attributes_for :associations, :reject_if => proc { |a| a['name'].blank? } end
협회 모델 :
class Association < ActiveRecord::Base has_and_belongs_to_many :attorneys accepts_nested_attributes_for :attorneys validates_presence_of :name, :message => "Please enter an association name." end
변호사 컨트롤러 : 여기
코드의 조각 내 삽입 시도에 해당하는 기록이다
def new
@attorney = Attorney.new
@attorney.associations.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @attorney }
end
end
def create
@attorney = Attorney.new(params[:attorney])
respond_to do |format|
if @attorney.save
flash[:notice] = 'Attorney was successfully created.'
format.html { redirect_to(@attorney) }
format.xml { render :xml => @attorney, :status => :created, :location => @attorney }
else
format.html { render :action => "new" }
format.xml { render :xml => @attorney.errors, :status => :unprocessable_entity }
end
end
end
,
변호사의 새보기 : 부분
<% form_for(@attorney, :html => {:multipart => true}) do |f| %>
<%= f.error_messages %>
<%= f.label :"First name" %>
<%= f.text_field :firstname %><br>
<%= f.label :"Last Name" %>
<%= f.text_field :lastname %><br>
<%= f.label :"Attorney Type" %>
<%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br>
<%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br>
<%= f.label :"Attorney Location" %>
<%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br>
<div id="associations">
<%= render :partial => 'shared/membership' %>
</div>
<%= add_association_link "Add Association" %>
<%= f.submit 'Create' %>
<% end %>
회원 :
<div class="association">
<% fields_for :associations do |assoc_form| %>
<%= assoc_form.collection_select(:association_id, Association.find(:all), :id, :name, :include_blank => true) %>
<% = link_to_function ") ($ (이) .up ('협회.')를 제거합니다.", "제거" %> <퍼센트 = LINK_TO '새 연결', new_association_path %의> <% 끝 %>
변호사 헬
def add_association_link(name)
link_to_function name do |page|
page.insert_html :bottom, :associations, :partial => 'shared/membership', :object => AssociationsAttorneys.new
end
end
가입 표 마이그레이션 :
class CreateAssociationsAttorneys < ActiveRecord::Migration
def self.up
create_table :associations_attorneys do |t|
t.references :attorney, :null => false
t.references :association, :null => false
t.timestamps
end
end
def self.down
drop_table :associations_attorneys
end
end
로그 캡처 : 링크 당
는Processing AttorneysController#new (for 127.0.0.1 at 2009-12-04 08:16:19) [GET]
Rendering template within layouts/default
Rendering attorneys/new
[4;35;1mMemberType Load (0.4ms)[0m [0mSELECT * FROM "member_types" [0m
[4;36;1mOfficeLocation Load (18.6ms)[0m [0;1mSELECT * FROM "office_locations" [0m
[4;35;1mAssociation Load (0.6ms)[0m [0mSELECT * FROM "associations" [0m
Rendered shared/_membership (3.5ms)
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM "associations" [0m
Rendered shared/_membership (1.5ms)
Rendered shared/_nav (0.6ms)
Rendered shared/_footer (0.1ms)
Completed in 149ms (View: 114, DB: 20) | 200 OK [http://localhost/attorneys/new]
Processing ApplicationController#index (for 127.0.0.1 at 2009-12-04 08:16:19) [GET]
Processing AttorneysController#create (for 127.0.0.1 at 2009-12-04 08:16:57) [POST]
Parameters: {"commit"=>"Create", "authenticity_token"=>"Jh7aMCcOY7jUu/D1YtiCswg2n6iwqnS98VnVn46psp0=", "associations"=>{"association_id"=>"3"}, "attorney"=>{"birthstate"=>"Alabama", "office_location_id"=>"1", "birthdate"=>"December 3, 2009", "birthcity"=>"Test", "middlename"=>"Test", "lastname"=>"Testing", "image_temp"=>"", "member_type_id"=>"2", "firstname"=>"Test", "bio"=>"testing testing testing", "suffix"=>"", "email"=>"[email protected]"}}
[4;35;1mAttorney Load (15.6ms)[0m [0mSELECT "attorneys".id FROM "attorneys" WHERE ("attorneys"."email" = '[email protected]') LIMIT 1[0m
[4;36;1mAttorney Create (0.8ms)[0m [0;1mINSERT INTO "attorneys" ("birthstate", "created_at", "birthdate", "office_location_id", "birthcity", "updated_at", "middlename", "lastname", "firstname", "member_type_id", "suffix", "bio", "image", "email") VALUES('Alabama', '2009-12-04 15:16:57', 'December 3, 2009', 1, 'Test', '2009-12-04 15:16:57', 'Test', 'Testing', 'Test', 2, '', 'testing testing testing', NULL, '[email protected]')[0m
Redirected to http://localhost:3000/attorneys/11
Completed in 150ms (DB: 16) | 302 Found [http://localhost/attorneys]
내가 볼 수있는 협회 "=> {"association_id "=>"3 "}이 그것을 특정 사용자에 대해 가지고있는 다중 연결의 마지막 만 가져오고 조인 테이블에 항목을 만들지 않습니다. 내 코드가 어디에서 잘못되었을 수 있습니까?
, 지금 오류를 얻고있다 : 정의되지 않은 <% = assoc_form.collection_select (: association_id, Association.find (: all), : id)이 줄의 # 에 대한 'association_id' , : 이름, : include_blank => true) %>. 어떤 아이디어? –
davidstites
다른 여러 문제를 간과 한 것처럼 보입니다. 나는 그 문제를 해결하기 위해 해결책을 업데이트했다. – EmFi
나는 몇 주 동안이 일을하고 아직도 문제가 있습니다. attorney_association 부분을 렌더링하려 할 때 'f'가 무엇인지 모르는 것에 대한 불평이 ... 나는 형식 객체가 : locals hash와 함께 전달되었다고 생각했습니다. – davidstites