2016-11-26 4 views
0

조인 테이블로 이동하는 선택 태그 목록을 만들어야합니다. 이 루프를 만들었습니다루프가있는 선택 태그 생성

- @game_details.fighting_game_main_characters.each do |main_character| 
    = simple_fields_for :combo_book_combo_works do |works| 
    = main_character.name 
    = works.input :fighting_game_main_character_id, as: :hidden, 
     input_html: {value: main_character.id} 
    = works.select :test, ["Works", "Doesn't Work", "Untested"], {}, 
     selected: "Untested", class: "form-control" 

추가하면 그것이 예상대로 나타납니다. 제출을 클릭하면 조인 테이블에 쓰지 않습니다. 나는 로그를 쳐다 보면서 마지막 선택 태그가 나는 페이지 소스를 보이는

Parameters: 
{ 
    "utf8"=>"✓", 
    "combo_book_combo"=>{ 
    "combo_book_combo_characters_attributes"=> 
     {"0"=>{"fighting_game_main_character_id"=>"1"} 
    }, 
    "combo_type"=>"Combo", 
    "character_positions"=>"Ground to Ground", 
    "screen_position"=>"Midscreen", 
    "combo"=>"ô" 
    }, 
    "keyboard-select"=>"", 
    "combo_book_combo_works"=>{ 
     "fighting_game_main_character_id"=>"13", 
     "test"=>"Works"}, 
    "commit"=>"Add combo" 
} 

제출 PARAMS 있다는보고 선택 모두 내가

id: "combo_book_combo_works_#{main_character.id}" 
를 추가하므로 동일한 ID를 가지고 것을보고

그러나 여전히 조인 테이블을 작성하지 않은으로 그 문제가 해결되지 않았다 그래서 나는 모델

class ComboBookCombo < ApplicationRecord 

    has_many :combo_book_combo_characters 
    has_many :fighting_game_main_characters, through: :combo_book_combo_characters 

    has_many :combo_book_combo_assists 
    has_many :fighting_game_assist_characters, through: :combo_book_combo_assists 

    has_many :combo_book_combo_character_extras 
    has_many :fighting_game_main_character_extras, through: :combo_book_combo_character_extras 

    has_many :combo_book_combo_works 
    has_many :fighting_game_main_characters, through: :combo_book_combo_works 

    has_many :combo_book_combo_extras 
    has_many :fighting_game_extras, through: :combo_book_combo_extras 

    validates :combo, :combo_type, :character_positions, :screen_position, presence: true 

    accepts_nested_attributes_for :combo_book_combo_characters, reject_if: proc { |attributes| attributes['fighting_game_main_character_id'].blank? }, allow_destroy: true 

    accepts_nested_attributes_for :combo_book_combo_assists, reject_if: proc { |attributes| attributes['fighting_game_assist_character_id'].blank? }, allow_destroy: true 

    accepts_nested_attributes_for :combo_book_combo_character_extras, reject_if: proc { |attributes| attributes['fighting_game_charater_extra_id'].blank? }, allow_destroy: true 

    accepts_nested_attributes_for :combo_book_combo_extras, reject_if: proc { |attributes| attributes['fighting_game_extra_id'].blank? }, allow_destroy: true 

    accepts_nested_attributes_for :combo_book_combo_works, reject_if: proc { |attributes| attributes['fighting_game_main_charater_id'].blank? }, allow_destroy: true 

    belongs_to :combo_book_user 
end 

class ComboBookComboWork < ApplicationRecord 
    belongs_to :combo_book_combo, optional: true 
    belongs_to :fighting_game_main_character 
end 


class FightingGameMainCharacter < ApplicationRecord 
    belongs_to :fighting_game 

    has_many :fighting_game_main_character_extras 

    has_many :combo_book_combo_characters 
    has_many :combo_book_combos, through: :combo_book_combo_characters 

    has_many :combo_book_combo_works 
    has_many :combo_book_combos, through: :combo_book_combo_works 

end 

다음 컨트롤러 보았다

,536,913,632
def create 
    @combo_book_combo = current_combo_book_user.combo_book_combos.build(combo_book_combo_params) 
    if @combo_book_combo.save 
     render 'index', notice: "Successfully created combo" 
    else 
     render 'index', notice: "Error creating combo" 
    end 
end 

(10)과 필요한 PARAMS

def combo_book_combo_params 
    params.require(:combo_book_combo).permit(
    :fighting_game_main_character_id, 
    :combo, 
    :combo_type, 
    :character_positions, 
    :combo_type, 
    :screen_position, 
    combo_book_combo_assists_attributes: [:id, :fighting_game_assist_character_id, :combo_book_combo_id, :_destroy], 
    combo_book_combo_character_extras_attributes: [:id, :combo_book_combo_id, :fighting_game_id, :_destroy], 
    combo_book_combo_extras_attributes:[:id, :combo_book_combo_id, :fighting_game_extra_id, :_destroy], 
    combo_book_combo_characters_attributes:[:id, :fighting_game_main_character_id, :combo_book_combo_id, :_destroy], 
    combo_book_combo_works_attributes:[:id, :combo_book_combo_id, :fighting_game_main_character_id, :test] 
) 
end 

모든 것이 그들과 함께 잘 될 것 같다하지만 난 다시 제출할 때 나는

ComboBookUser Load (0.5ms) SELECT "combo_book_users".* FROM "combo_book_users" WHERE "combo_book_users"."id" = $1 ORDER BY "combo_book_users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]] 
    (0.1ms) BEGIN 
    SQL (0.6ms) INSERT INTO "combo_book_combos" ("combo", "combo_type", "character_positions", "screen_position", "created_at", "updated_at", "combo_book_user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["combo", "ô"], ["combo_type", "Combo"], ["character_positions", "Ground to Ground"], ["screen_position", "Midscreen"], ["created_at", 2016-11-26 19:48:19 UTC], ["updated_at", 2016-11-26 19:48:19 UTC], ["combo_book_user_id", "2"]] 
    SQL (0.4ms) INSERT INTO "combo_book_combo_characters" ("combo_book_combo_id", "fighting_game_main_character_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["combo_book_combo_id", 52], ["fighting_game_main_character_id", 1], ["created_at", 2016-11-26 19:48:19 UTC], ["updated_at", 2016-11-26 19:48:19 UTC]] 
    (2.3ms) COMMIT 
    Rendering combo_book_combos/index.html.haml within layouts/application 
    Rendered combo_book_combos/index.html.haml within layouts/application (1.6ms) 
Completed 200 OK in 76ms (Views: 61.7ms | ActiveRecord: 4.0ms) 

상영하지 않습니다에 기록 될 그 필요에 가입 얻을 쪽으로. 루프가 생성 한 모든 선택 태그를 조인 테이블에 쓰려면 어떻게해야합니까?

+0

''combo_book_combo_works ''매개 변수는''combo_book_combo ''매개 변수의 일부로 중첩되지 않습니다. 작업 속성이 combo_book_combo 매개 변수와 동일한 해시의 일부로 전달되도록 양식 코드를 다시 작성해야합니다. – guiniveretoo

+0

combo_book_combo 매개 변수를 추가했습니다. 내 양식을 어떻게 다시 작성해야한다고 생각하니? – chun

답변

0

컨트롤러는 모든 매개 변수가 combo_book_combo 값 아래의 중첩 값으로 전달 될 것으로 예상합니다.

{ 
    "combo_book_combo"=>{ 
     "combo_book_combo_characters_attributes"=>{ 
      "0"=>{"fighting_game_main_character_id"=>"1"} 
     }, 
     "combo_type"=>"Combo", 
     "character_positions"=>"Ground to Ground", 
     "screen_position"=>"Midscreen", 
     "combo"=>"ô", 
     "combo_book_combo_works_attributes"=>{ 
      "fighting_game_main_character_id"=>"13", 
      "test"=>"Works" 
     }, 
    }, 
} 
:

{ 
    "combo_book_combo"=>{ 
     "combo_book_combo_characters_attributes"=>{ 
      "0"=>{"fighting_game_main_character_id"=>"1"} 
     }, 
     "combo_type"=>"Combo", 
     "character_positions"=>"Ground to Ground", 
     "screen_position"=>"Midscreen", 
     "combo"=>"ô" 
    }, 
    "combo_book_combo_works"=>{ 
     "fighting_game_main_character_id"=>"13", 
     "test"=>"Works" 
    }, 
} 

이 컨트롤러 당신의 매개 변수의 모습 기대 것입니다 :

def combo_book_combo_params 
    params.require(:combo_book_combo).permit(
    :fighting_game_main_character_id, 
    :combo, :combo_type, 
    :character_positions, 
    :combo_type, 
    :screen_position, 
    combo_book_combo_assists_attributes: [ 
     :id, 
     :fighting_game_assist_character_id, 
     :combo_book_combo_id, 
     :_destroy], 
    combo_book_combo_character_extras_attributes: [ 
     :id, 
     :combo_book_combo_id, 
     :fighting_game_id, 
     :_destroy], 
    combo_book_combo_extras_attributes:[ 
     :id, 
     :combo_book_combo_id, 
     :fighting_game_extra_id, 
     :_destroy], 
    # etc 
    ) 
end 

이 당신의 매개 변수가 현재 어떤 모습인지입니다 : 즉,이 방법이해야 할 노력하고 무엇

combo_book_combo_works안에 있으며combo_book_combo 매개 변수 키가 combo_book_combo_works_attributes으로 변경되었습니다.

양식을 지금 보시고, 특히 combo_book_combo_characters_attributes의 비트를 살펴보십시오. 양식의 일부가 중첩 된 양식 속성으로 올바르게 구성되어있는 것 같습니다. simple_fields_for을 변경하여 독립 실행 형 뷰 도우미 메서드 호출 대신 상위 폼 (f.simple_fields_for과 같은 것)에서 호출되는 메서드로 변경해야합니다.

+0

'= f.simple_fields_for : combo_book_combo_works do | works | '를 추가했으나 모든 이름과 선택 태그는 없어졌습니다. – chun

+0

'콤보 북 콤보'개체에 초기화되거나 저장된 "작업"이 있습니까?Simple_fields_for는 기존 객체를 반복합니다. 존재하는 객체가 있으면 그 객체를 반복하지만 빈 객체는 초기화하지 않습니다. 양식 필드가 표시 될 수 있도록 'work'객체를 작성해야 할 수도 있습니다. – guiniveretoo

+0

그것은'- @ game_details.fighting_game_main_characters.each do | main_character |'와'= f.simple_fields_for : combo_book_combo_works do | works | '를 바꾸었고 모든 이름과 선택 태그가 나타나지만 아직 조인 테이블에 쓰지 않고 오직 마지막 선택 태그가 전달 된 매개 변수에 표시됩니다. ' – chun