2010-01-20 2 views
0

У меня есть модель комнаты и модель Person.accepts_nested_attributes_for и выберите тег

В комнате может быть много человек, и человек может иметь одну комнату.

На экране создания комнаты я могу "ссылка н люди в этой новой комнате Так я хочу, чтобы иметь переменное количество выбора тега, который содержит список всех людей

Я не знаю, как создайте тег select.

Может кто-нибудь помочь

спасибо ...

Я следующие ассоциации

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

Nice ... и быстрый ответ спасибо!

здесь тестируемый код правильным

<% 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 %>