2013-07-03 3 views
0

컨트롤러AssociationTypeMismatch

class PlayerProfilesController < InheritedResources::Base 

    def show 
     @player_profile = PlayerProfile.find(params[:id]) 
    end 
end 

모델

class PlayerProfile < ActiveRecord::Base 

    has_many :playing_roles, :dependent => :destroy 
    has_many :player_roles, through: :playing_roles 

end 

class PlayerRole < ActiveRecord::Base 

    has_many :playing_roles, :dependent => :destroy 
    has_many :player_profiles, through: :playing_roles 

end 

class PlayingRole < ActiveRecord::Base 
    belongs_to :player_profile 
    belongs_to :player_role 

end 

show.html.erb

<%=collection_check_boxes(:player_profile, :playing_roles, PlayerRole.all, :id, :name)%> 

collection_check_boxes (문서)

HTML 두 개의 체크 박스

<input id="player_profile_playing_roles_1" name="player_profile[playing_roles][]" type="checkbox" value="1" class="hidden-field"> 
<span class="custom checkbox checked"></span> 
<label for="player_profile_playing_roles_1">Striker</label> 

<input id="player_profile_playing_roles_2" name="player_profile[playing_roles][]" type="checkbox" value="2" class="hidden-field"> 
<span class="custom checkbox"></span> 
<label for="player_profile_playing_roles_2">Midfielder</label> 
<input name="player_profile[playing_roles][]" type="hidden" value=""> 

생성 제대로 모두 표시 것 같다하지만 난 제출 버튼을 클릭 할 때 나는이 오류 얻을 : enter image description here

+0

당신이 컨트롤러 액션 코드를 추가 할 수 있습니까? –

+0

@MichaelSzyndel done;) – sparkle

+0

@ user1028100 전체 백 트레이스를 붙여 넣을 수 있습니까? –

답변

4

미안, 나는이 생각 복잡했다. 그러나 나는 그것이 있다고 생각하지 않는다.

:playing_roles이 표시되지만, PlayerRole.all을 통해 PlayerRoles 컬렉션을 전달한다고 말합니다. 그것은 불일치입니다. AssociationTypeMismatch는 개체에 오리를 연결하고 헬리콥터를 전달하도록 지시하는 경우입니다.

는이 작업을 수행해야합니다

<%= collection_check_boxes(:player_profile, :player_role_ids, PlayerRole.all, :id, :name) %> 

당신은 그것을 :player_role_ids을 기대하는 말, 당신은 가치 방법 :id 및 텍스트 방법 :name로에게 PlayerRole.all의 컬렉션을 전달합니다.

그런 다음 업데이트시 해당 ID가 Player의 player_role_ids 속성에 저장되어 연결이 생성됩니다.

은 참조 : Rails has_many :through and collection_select with multiple