2010-01-20 4 views
0

방 모델과 사람 모델이 있습니다.accepts_nested_attributes_for 및 select 태그

방은 많은 사람을 가질 수 있으며 사람은 한 방을 가질 수 있습니다. 방 생성 화면에서

나는 '링크'N이 새로운 방 에 사람이 그래서 모든 사람들의 목록이 포함 select 태그의 변수 양을 갖고 싶어

나는 방법을 모른다 할 수 select 태그를 빌드하십시오.

사람이

감사를 도울 수

...

I/새로운 형태의 다음과 같은 협회가

class Room < ActiveRecord::Base 
    has_many :people 
    accepts_nested_attributes_for :people 
end 

class Person < ActiveRecord::Base 
    belongs_to :room 
end 

내가 방을 구축 할 부분을 사용했습니다

<% form_for(@room) do |f| %> 
    <%= f.error_messages %> 
    <p> 
    <%= f.label :date %><br /> 
    <%= f.date_select :date %> 
    </p> 
    <% f.fields_for :people do |builder| %> 
    <p> 
     <%= builder.label :person_id, "Person" %><br /> 
     <%= select 'room', 'people', Person.all.collect{|person| [person.name, person.id]}%> 
    </p> 
    <% end %> 
    <p> 
    <%= f.label :comment %><br /> 
    <%= f.text_area :comment %> 
    </p> 
    <p> 
    <%= f.submit 'Create' %> 
    </p> 
<% end %> 

답변

2

니스 ... 빠른 답변 감사합니다! 여기

이 올바르지하는 테스트 코드입니다

<% f.fields_for :people do |builder| %> 
    <p> 
     <%= builder.label :person_id, "Person" %><br /> 
     <%= builder.collection_select :person_id, Person.all, :id, :name, {:multiple => true} %> 
    </p> 
    <% end %> 
2

ActionView::Helpers::FormOptionsHelper 모듈을 살펴보십시오. collection_select 메소드를 제공합니다.

이 부분은 당신이 찾고있는 무엇을 수행합니다

<% form_for(@room) do |f| %> 
    <%= f.error_messages %> 
    <p> 
    <%= f.label :date %><br /> 
    <%= f.date_select :date %> 
    </p> 
    <p> 
    <%= f.label :people_ids, "Person" %><br /> 
    <%= collection_select :people_ids, Person.all, :name, :id, {:multiple => true} %> 
    </p> 

    <p> 
    <%= f.label :comment %><br /> 
    <%= f.text_area :comment %> 
    </p> 
    <p> 
    <%= f.submit 'Create' %> 
    </p> 
<% end %> 
관련 문제