2013-06-11 3 views
0

그룹이 시스템에서 사용할 수있는 여러 모듈을 선택할 수있는 양식을 만들려고합니다. 불행히도 이것을 시도 할 때마다 미친 연관 오류가 계속 발생하고 이유가 없습니다. 여기 has_many 관계 루비를 사용하여 폼 만들기

내 양식을 여기

<%= form_for @group, :url => admin_groups_path, :html => { :class => 'entry' } do |f| %> 
    <%= render :partial => 'shared/error_messages', :locals => { :object => @group } %> 

    <div class="g12"> 
    <fieldset> 
     <fieldset> 
     <legend>Add Group</legend> 
     <div class="simple_format"> 
      <section class="g3"> 
      <%= f.label :name %> 
      <div><%= f.text_field :name, :size => 15 %></div> 
      </section> 
      <section class="g3"> 
      <%= f.label :is_facility, 'Facility?' %> 
      <div style="display:block;margin: 0 auto;width:25px;"><%= f.check_box :is_facility %></div> 
      </section> 
      <section class="g3"> 
      <%= f.label :parent_id %> 
      <div style="display:block;margin: 0 auto;width:25px;"><%= f.collection_select :parent_id, current_group.self_and_descendants.reject(&:is_facility), :id, :name %></div> 
      </section> 
      <section class="g3"> 
      <%= f.label :time_zone, 'Time Zone' %> 
      <div><%= f.select :time_zone, ActiveSupport::TimeZone.all.map(&:name).select {|x| x=~ /US/} %></div> 
      </section> 
     </div> 
     <div class="simple_format"> 
      <section class="g3"> 
      <%= f.label :federal_tax_number, 'Federal Tax ID' %> 
      <div><%= f.text_field :federal_tax_number, :size => 15 %></div> 
      </section> 
      <section class="g3"> 
      <%= f.label :national_provider_identifier, 'National Provider ID' %> 
      <div><%= f.text_field :national_provider_identifier, :size => 15 %></div> 
      </section> 
      <section class="g3"> 
      <%= f.label :modules, 'Allowed Modules' %> 
      <div><%= f.collection_select :group_access_modules, AccessModule.all, :id, :name, {}, {:multiple => true} %></div> 
      </section> 
     </div> 
     <section class="g12"> 
      <div><%= f.submit "Add Group", { :class => 'form-submit' } %></div> 
     </section> 
     </fieldset> 
    </fieldset> 
    </div> 
<% end %> 

의 코드 것은 내가 완전히 지금 분실하고 내가

<h1> 
    ActiveRecord::AssociationTypeMismatch 
    in Admin::GroupsController#create 
</h1> 
<pre>GroupAccessModule(#70245162706560) expected, got String(#70245155065480)</pre> 

저장에 얻을 오류 내 모델 여기

class Group < ActiveRecord::Base 
    has_many :group_access_modules 
end 

입니다 .

+0

GroupsController 코드도 게시하십시오. – vee

답변

0

어떻게 생각하는지 알려 주시기 바랍니다. 그것은 다음과 비슷한 모습이 될 것입니다

<%= f.fields_for :group_access_modules do |g| %> 
    <%= g.collection_select(:group_id, AccessModule.all, :id, :name, {}, {:multiple => true} %> 
<% end %> 

참고 : 당신은 틀에 얽매이지 않는 외래 키 이름을 사용하는 경우 방법 (:group_id)을 변경해야 할 수도 있습니다.

0

simple_form을 사용해 보셨습니까? 분명히

<%= f.association :company %> 

이 코드를 읽기 쉽게하며, 그것은 꽤 구성입니다 : 예를 들어 아주 쉽게 많은 관계를 가지고 것이 연결을합니다.

railscast on habtm checkboxes도 있지만 이것은 구독이 필요하다고 생각되는 수정 된 에피소드입니다.

날 당신이 당신의 양식에 연관에 대해 fields_for를 사용할 수있는 simple_form 보석