2013-07-02 3 views
3
class PlayerProfile < ActiveRecord::Base 

    has_many :playing_roles 
    has_many :player_roles, through: :playing_roles 

    accepts_nested_attributes_for :playing_roles, :allow_destroy => true 

end 

class PlayingRole < ActiveRecord::Base 
     belongs_to :player_roles 
     belongs_to :player_profile 
end 

class PlayerRole < ActiveRecord::Base 
    has_many :playing_roles 
    has_many :player_profiles, through: :playing_roles 
end 

Schema.rb체크 박스 대다 나는 각각의 역할 플레이어가 할 수있는 연주에 대한 확인란을 보여줄 필요가 관계

create_table "player_profiles", force: true do |t| 
    t.integer "user_id" 
    t.string "firstname" 
    t.string "lastname" 
    t.date  "birthdate" 
    t.string "favorite_team" 
    t.string "mobile" 
    t.string "address" 
    t.string "lang" 
    t.string "team" 
    t.integer "weight" 
    t.integer "height" 
    t.text  "biography" 
    t.string "idols" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "nationality" 
    end 
    add_index "player_profiles", ["user_id"], name: "index_player_profiles_on_user_id", using: :btree 
    create_table "player_roles", force: true do |t| 
    t.string "name" 
    end 
    create_table "playing_roles", force: true do |t| 
    t.integer "player_profile_id" 
    t.integer "player_role_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

. 확인 체크 박스는 "playing_roles"relashionship

collection_check_boxes 사용에 대한 기록을 의미 Rails4에서

UPDATE

내가 사용하는 경우 :

<%=collection_check_boxes(:player_profile, :playing_role_ids, PlayerRole.all, :id, :name)%>  
: playing_role_ids
을 나는이 오류

enter image description here

관계에 대한 레코드를 찾는 것 같지만 레코드가 존재하지 않는다면 관계가 없으며 선택란을 선택 해제해야 함을 의미합니다. http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_check_boxes 레일 3.X에서

+0

simpleform 또는 formtastic과 같은 것을 사용하고 있습니까? 아니면 정상적인 레일이 헬퍼를 형성합니까? –

+0

일반 레일 헬퍼. Formstatic/simpleform이이 경우 더 좋을 수 있습니까? – sparkle

+0

내 대답 –

답변

0

것은, 당신은 collection_check_boxes이 사용할 수 있어야

collection_check_boxes 쓰기

를 사용하여 레일 4에서
<%= simple_form_for @player_profile do |f| %> 
    <%= f.association :player_roles, as: :check_boxes %> 
    ... 
<% end %> 

,

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

ID를 할당하고 있기 때문에 PlayingRoles가 아니기 때문에 :playing_roles_ids 이름을 사용해야합니다.

+0

완료 힌트를 추가했습니다. 그러나 나는 오류가 있습니다. 질문 업데이트 – sparkle

5

, simple_form을 사용합니다 : 당신이 레일 4를 사용하는 경우

+0

시도해 보았지만 다음과 같이 나타납니다. http://cl.ly/image/241d200o0z3J – sparkle

+0

마지막 편집을 시도했지만 오류가 발생합니다. 질문이 업데이트되었습니다. – sparkle

+0

죄송합니다. 복수형이 잘못되었습니다. 'playing_role_ids', singular playing_role, pluralids가 있습니다. –