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

添加它如預期顯示。當我點擊提交它不寫入連接表。我看了看日誌,發現只有最後選擇的標籤是在提交PARAMS

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" 
} 

我看上去頁面的源代碼,看到所有的選擇中具有相同的ID,所以我說

id: "combo_book_combo_works_#{main_character.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 

,然後控制器

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 

和所需的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」'params的一部分嵌套的。您需要重寫表單代碼,以便將作品屬性作爲combo_book_combo參數的相同散列的一部分傳遞。 – guiniveretoo

+0

我添加了combo_book_combo參數。你覺得我應該重做我的表格嗎? – chun

回答

0

您的控制器期望所有參數都作爲combo_book_combo值下的嵌套值傳入。這就是這種方法試圖做的事:

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"=>{ 
     "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" 
    }, 
} 

這是你的控制器預計您的參數看起來像:

{ 
    "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_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將遍歷任何現有的對象(如果有),但它不會初始化一個空白對象 - 您可能需要構建一個「工作」對象,以便顯示錶單字段。 – guiniveretoo

+0

它交換了' - @ game_details.fighting_game_main_characters.each do | main_character |'和'= f.simple_fields_for:combo_book_combo_works do | works |'並且所有名稱和選擇標籤都顯示出來,但它仍然不寫入連接表最後一個選擇標籤顯示在傳遞的參數中。 ' – chun