使用循环创建选择标记的问题

时间:2016-11-26 20:19:57

标签: ruby-on-rails simple-form simple-form-for

我需要创建一个进入连接表的选择标记列表。我创建了这个循环

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

我看起来是一个页面源,看到所有的选择都有相同的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

和所需的参数

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)

需要写入的联接未显示。我需要做些什么才能使循环生成的所有选择标签写入连接表?

1 个答案:

答案 0 :(得分: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参数,密钥已更改为combo_book_combo_works_attributes

立即查看您的表单,尤其是combo_book_combo_characters_attributes的位 - 看起来表单的一部分是使用嵌套表单属性正确构建的。您可能会更改simple_fields_for,以便它是在父表单上调用的方法(类似于f.simple_fields_for),而不是独立的视图帮助方法调用。