나는 Company와 CompanyContact의 두 모델을 가지고 있습니다. 나는 이미 회사에 대한 회사 연락처가있는 경우Rails 3. 하나의 중첩 모델 양식 만 표시하려면 어떻게해야합니까?
그래서 내가 평소 가지고 ... 편집 양식에서
companies_controller.rb
def edit
@company = Company.find(params[:id])
student = @company.students.build
company_contact = @company.company_contacts.build
end
company.rb
has_many :company_contacts, :dependent => :destroy
accepts_nested_attributes_for :company_contacts, :reject_if => :reject_company_contacts, :allow_destroy => true
company_contact.rb
belongs_to :company
form.html.erb
<%= f.fields_for :company_contacts do |builder| %>
<%= render "company_contact_fields", :f => builder %>
<% end %>
_company_contacts.html.erb
<p style="margin:5px 0;">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, :class => 'text_field' %>
</p>
<p style="margin:5px 0;">
<%= f.label :email %><br />
<%= f.text_field :email, :class => 'text_field' %>
</p>
, 그것은 나에게주는 기존의 기록 플러스가 될 준비가 다른 비어있는 회사 연락처 양식 작성. 그것은 그것이 일어나기로되어 있기 때문에 괜찮습니다.
내가해야 할 일은 회사 연락처가 하나뿐이므로 이미 회사 연락처가있는 경우 다른 회사 연락처를 추가하는 다른 양식을 표시하고 싶지 않습니다. 내 클라이언트가 미래에 추가 회사 연락처를 추가 할 수도 있기 때문에 has_one 관계를 설정하고 싶지 않으며 has_one 관계를 시도했을 때 많은 오류가 발생했습니다.
회사마다 단 하나의 회사 연락처 만 있으면 카운터 솔루션을 시도했지만 루프에서 카운터 = 0을 설정 한 다음 카운터> 0인지 확인하지만 "솔루션"이 작동하지 않습니다. 너는 무엇을 제안 하겠는가?