我最近尝试在使用Postgres 9.3的Rails应用程序中添加一个Array类型列。我按照一些在线教程,将以下代码添加到我的项目中。
移植
add_column :matches, :p1_characters, :string, array: true, default: []
add_column :matches, :p2_characters, :string, array: true, default: []
现在,如果我尝试手动更新控制台中的匹配字符,如下所示:
> m = Match.first
> m.p1_characters = ["1","2","3"]
> m.save
# Works fine!
所有内容都按预期正确保存在数据库中。
问题在于尝试更新rails应用程序中的p1_characters / p2_characters属性。
p1_edit_character_.html.erb
<%= 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
以下是我的控制器的相关方法和强大的参数 的 matches_controller.rb
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
请,任何指导都会非常有用。
感谢。