2016-11-26 18 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" 

それが期待どおりに表示されます。私が提出したとき、それは結合テーブルに書き込まれませんでした。私は、ログを見て、唯一の最後のselectタグは、私はページのソースを探します

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 

と、コントローラ

見結合テーブルには書いていないように、その問題を解決しませんでした10
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 

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 

のparams必要な、すべてが彼らと罰金のようですが、私は再び提出するとき、私はに書き込まれることの必要性を参加

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 ''のparamsは '' combo_book_combo "' paramsの一部としてネストされていないようです。作品属性が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 params、およびキーが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

関連する問題