4
has_many : through 관계를 처리하고 렌더링 된 중복 필드를 가져 오는 중첩 된 양식을 만들려고합니다.fields_for를 사용할 때 중복 된 양식 필드
모델
회사
has_many :provides
has_many :services, :through => :provides
accepts_nested_attributes_for :services, :provides
attr_accessible :service_ids
서비스
belongs_to :company
belongs_to :service
제공
has_many :provides
has_many :companies, :through => :provides
has_many :portfolio_items
acts_as_nested_set
컨트롤러
설정/서비스
def index
@company_services = @company.services
@service_list = Service.where("parent_id IS NULL")
end
def show
@user = current_user
# Find features for supplier based users
unless @company.blank?
@my_sectors = @company.sectors
end
end
def update
if params[:company].nil?
@company.service_ids = nil
end
respond_to do |format|
if @company.update_attributes(params[:company])
format.html { redirect_to settings_path, :notice => "Services successfully updated" }
else
format.html { render :index }
end
end
end
조회수
양식 -
<%= form_for @company, :url => settings_service_path(@company), :method => :put do |f| %>
<div>
<ul>
<% @service_list.each do |item| %>
<%= f.fields_for :provides do |p| %>
<%= p.fields_for :services do |s| %>
<%= render :partial => "subs", :locals => {:subs => s, :service => item, :f => f, :p => p } %>
<% end %>
<% end %>
<% end %>
</ul>
<%= submit_tag("Update") %>
<% end %>
_subs.html.erb
<li>
<%= check_box_tag :service_ids, service.id, @company.services.include?(service), :name => "company[service_ids][]", :class => "checkbox" %>
<%= label_tag service.id, service.service_name %>
<% unless service.children.blank? %>
<ul>
<%= render :partial => "subs", :collection => service.children %>
</ul>
<% end %>
</li>
fields_for가 중복을 일으키는 것을 알고 있지만 그 이유를 모르겠습니다.
아무도 분명히 할 수 있습니까?
당신은 쓸 필요가
3 레코드를 저장하면 각 행이 3 번 반복됩니다. –
좋아요, 각 루프 바깥에서 fields_for를 이동 한 후에 좀 더 명확하게 이해할 수 있습니다. fields_for는 기존 관계도 루핑합니다. 이 확인란을 선택했는지 여부에 관계없이 모든 서비스 확인란을 표시해야하므로이 기능이 작동하지 않습니다. –