2014-02-22 6 views
0

formtastic을 필자의 레일 프로젝트에 성공적으로 통합하여 다 대다 관계에서 다중 값 선택을 수행하는 데 사용했습니다.Formtastic 컬렉션을 many에서 many로 제한하기

문제는 관련있는 항목으로 선택을 제한하는 방법을 찾지 못하는 것입니다.

다음 감안할 때 :

class Pool < ActiveRecord::Base 
has_many :poolings, dependent: :destroy 
has_many :commercials, :through => :poolings 
accepts_nested_attributes_for :poolings 
accepts_nested_attributes_for :commercials 
belongs_to :client 
end 

의 관계 다 대다 모델

class Pooling < ActiveRecord::Base 
belongs_to :pool 
belongs_to :commercial 
end 

class Commercial < ActiveRecord::Base 
belongs_to :client 
has_many :poolings, dependent: :destroy 
has_many :pool, :through => :poolings 
end 

다음 코드는 여러 선택 만들 작동 상업 모델을 양식 :

<%= semantic_form_for @pool do |f| %> 
    <%= f.inputs %> 
    <%= f.inputs :commercials %> 
    <%= f.actions %> 
<% end %> 

핵심은 formtastic의 가정으로 : 광고는 다 대다 선택 양식으로 변환됩니다.

지금, 설명서를 읽고 난,

나는 형태를 사용할 때 "풀"을 편집 할 수 있도록
<%= f.inputs :commercials %> 

곳 "CLIENT_ID = 1"라인에 추가 할 것을 이해할 수 없었다 그것 만은 "광고"를 보여줘 것 "CLIENT_ID = 1 사용

선택 컬렉션 다음은 나에게 의도 된 결과를 제공합니다.

<%= f.collection_select :commercials, Commercial.where(:client_id => @pool.client), :id, :name %> 

formtast에 해당하는 무엇을 ic?

그리고 BTW, RoR을 처음 접했을 때 나는 다양한 구성 요소와 보석이 모두 약간 다르게 작동하는 것처럼 보였습니다. 오픈 소스의 기쁨!

답변

0

선택, 체크 박스 및 라디오에 대한 "수집"매개 변수가 있습니다 (Usage 참조).

<%= semantic_form_for @pool do |f| %> 
    <%= f.inputs do %> 
    <%= f.input :commercials, :collection => Commercial.where(:client_id => @pool.client) %> 
    <% end %> 
    <%= f.actions %> 
<% end %> 
: 특정 문제에 대한

이 솔루션은 다음과 같을 수 있습니다