1

각 이벤트에 여러 테이블이 있고 각 테이블에 여러 명이 앉아 있고 테이블에 앉아있는 테이블에 사람들을 매핑하는 여러 티켓이있는 이벤트 앱을 만들려고합니다. > 이것을 달성하기 위해 fields_for에 중첩 된 체크 박스를 만들었습니다 : 테이블 (차례대로 이벤트 양식에 있음) 강력한 매개 변수 나 양식 자체에 문제가 있다고 생각하지만 어떤 것도 찾을 수 없었습니다 어떤 사람이이 테이블에 앉아서 양식을 제출하고 양식으로 돌아가는지를 나타내는 양식의 확인란을 확인한 후 확인란이 더 이상 선택되지 않는다는 것을 알 수 있습니다. 여기 Rails의 중첩 체크 박스

내 모델의 내용이 여기에

# models 
class Event < ActiveRecord::Base 
    has_many :tables, dependent: :destroy 
    has_many :people , through: :tickets 
    has_many :tickets 
    accepts_nested_attributes_for :tickets, allow_destroy: true 
    accepts_nested_attributes_for :tables, allow_destroy: true 
end 

class Table < ActiveRecord::Base 
    belongs_to :event 
    has_many :tickets 
    has_many :people, through: :tickets 
end 

class Ticket < ActiveRecord::Base 
    belongs_to :table 
    belongs_to :person 
end 

class Person < ActiveRecord::Base 
    has_many :tickets 
    has_many :tables, through: :tickets 
end 

을 파일 간결함을 위해 생략 부품 형태입니다 있습니다. 여기

<%= form_for(@event) do |f| %> 
    ... 
    <%= f.fields_for :tables do |builder| %> 
    <%= render 'table_field', f: builder %> 
    <% end %> 
    <%= link_to_add_fields "Add Table", f, :tables %>  
    ... 
<% end %> 

그리고

내가 table_field 내에서 구현 한 체크 박스의 목록입니다.

<% Person.all.each do |person| %> 
      <div class="field">  
       <%= check_box_tag "table[people_ids][]", person.id, f.object.people.include?(person) %> <%= f.label [person.first_name, person.last_name].join(" ") %> 
      </div> 
      <% end %> 

이이 event_params

def event_params 
     params.require(:event).permit(:name, :description, :start, :end, :latitude, :longitude, :address, :data, :people_ids => [], tables_attributes: [:id, :number, :size, :people_ids => []]).tap do |whitelisted| 
    whitelisted[:data] = params[:event][:data] 
    end 

입니다 어떻게 체크 박스가 지속적으로이 양식 확인하기 위해 어떻게해야합니까?

답변

0

당신은 그것을뿐만 아니라 데이터를 유지합니다 http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes

<%= f.collection_check_boxes(:people_ids, Person.all, :id, :name) do |person| %> 
    <%= person.label { person.check_box } %> 
<% end %> 

를 사용할 수 있습니다.

+0

#

+0

에 대해 정의되지 않은 메소드'people_ids '에 오류가 발생했습니다.'<% = collection_check_boxes (: person, : people_ids, Person.all, : id, : first_name) do | 사람 | %>'그러나 체크리스트는 여전히 제출하고 새로 고침 할 때 작동하지 않습니다 ??? –

+0

그럴 필요는 없지만 해킹을 사용할 수는 있습니다. 위의 코드에서 <% if f.object.people.include? (person) %> <% = check_box_tag "table [people_ids] []", person.id, checked : "확인"%><% = f .label [person.first_name, person.last_name] .join ("") %> <% else %> <% = check_box_tag "table [people_ids] []", person.id %><% = f.label [person.first_name , person.last_name] .join ("") %> <% end %> 이벤트에 참석 한 사람들이 체크 박스에 체크되어 있는지 확인하십시오. –