Arrays Rails 4 PostgreSQL数组类型问题-Fixnum的未定义方法“gsub”

Arrays Rails 4 PostgreSQL数组类型问题-Fixnum的未定义方法“gsub”,arrays,postgresql,ruby-on-rails-4,simple-form,Arrays,Postgresql,Ruby On Rails 4,Simple Form,我最近尝试在我的Rails应用程序中添加一个数组类型的列,该应用程序使用Postgres 9.3。我学习了一些在线教程,并将以下代码添加到我的项目中 迁移 现在,如果我尝试在控制台中手动更新匹配的字符,如下所示: > m = Match.first > m.p1_characters = ["1","2","3"] > m.save # Works fine! 一切都按预期正确保存在数据库中 问题在于尝试更新我的rails应用程序中的p1_字符/p2_字符属性 p1_编辑_字

我最近尝试在我的Rails应用程序中添加一个数组类型的列,该应用程序使用Postgres 9.3。我学习了一些在线教程,并将以下代码添加到我的项目中

迁移

现在,如果我尝试在控制台中手动更新匹配的字符,如下所示:

> m = Match.first
> m.p1_characters = ["1","2","3"]
> m.save
# Works fine!
一切都按预期正确保存在数据库中

问题在于尝试更新我的rails应用程序中的p1_字符/p2_字符属性

p1_编辑_字符_uu.html.erb

我得到的错误是

undefined method `gsub' for 1:Fixnum
下面是我的控制器和相关的方法和强大的参数 匹配\u controller.rb

请,任何指导都会很有帮助


谢谢。

你好。将数组作为强参数传递可能会把您搞砸。根据,在match_params中,显式地将:p1_字符作为参数中的数组。require:match.permit:p1_分数,:p2_分数,:match_日期,:p1_接受,:p2_接受,:p1_字符=>[],:p2_字符Hey BenU,我现在遇到了一个新错误。我在强参数中添加了{p1_characters:[]},但现在出现了以下错误:44:FixnumYou的未定义方法'gsub'。你试过用谷歌搜索你的新错误信息吗?似乎有很多帖子对你的新帖子有深入的了解。这是一个rails bug
<%= simple_form_for(@match, method: :patch,
                           :url => p1_set_character_match_path(@match)) do |f| %>

# displays a form of check boxes where the user can select multiple characters.

  <h1>Select P1 Character</h1>
  <%= f.input :p1_characters, label: "Player 1 Character", as: :check_boxes, 
                              collection: @characters %>
  <%= f.button :submit, "Select Character", class: "btn-lg btn-primary" %>
<% end %>
"match"=>{"p1_characters"=>["1", "2", "3", ""]}
undefined method `gsub' for 1:Fixnum
def p1_edit_character
  @match = Match.find(params[:id])
  @characters = Game.find(@match.game_id).characters
end

def p1_set_character
  @match = Match.find(params[:id])
  if @match.update_attributes(match_params)
    if @match.p1_characters != []
    flash[:notice] = "P1 character set."
  end
    redirect_to matches_path
  end
end

private
  def match_params
    params.require(:match).permit(:p1_score, :p2_score, :match_date, 
    :p1_accepted, :p2_accepted, {p1_characters: []}, {p2_characters: []},
    :disputed, :finalized_date)
  end