过去两天一直在这,并无法弄清楚我做错了什么。我正在尝试使用form_tag
同时更新屏幕上的多个match_pick对象。用户注册后立即创建裸match_pick
条记录。然后我希望用户能够更新这些记录的相关信息并按下提交按钮。这是我到目前为止所做的:
的routes.rb
post "match_picks/adjust_match_picks" => "match_picks#adjust_match_picks", :as => :adjust_match_picks
match_picks_controller.rb
def adjust_match_picks
puts "The update_match_picks method has been called"
@match_picks_recd = params[:match_picks]
puts @match_picks_recd.inspect
puts "The parameters are:"
puts params.inspect
puts "The object passed is:"
end
视图/ match_picks / index.html.erb
<%= form_for adjust_match_picks_path, :method => :post do %>
<% @match_picks.each do |pick| %>
<%= fields_for "match_picks[]", pick do |mp| %>
<%= mp.text_field :id %>
<%= mp.text_field :result_pick %>
<% end %>
<% end %>
上面生成的HTML如下: -
<form accept-charset="UTF-8" action="/match_picks" method="post"><div style="display:none"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="EIEFdhjkkfnkjfYjlkjdlkjCzxaxHeaXbkvu0wbLB1QsOj+o=" /></div>
以上代码出现以下错误:
ActionController::ParameterMissing in MatchPicksController#create
param is missing or the value is empty: match_pick
然后重点介绍这一段
def match_pick_params
params.require(:match_pick).permit(:user_id, :match_id, :round_id, :result_pick, :team_id, :try_pick, :margin_id, :result_points, :try_points, :margin_points, :random, :closed)
end
我在match_picks控制器中的每个方法中都进行了一些调试,表单提交似乎触发了控制器的create
方法而不是我的自定义adjust_match_picks
方法。它看起来像错误是触发因为创建方法没有被传递match_pick_params
。但是我不想触发create
方法,我希望自定义adjust_match_picks
触发。
我还没有输入任何代码来处理传递给adjust_match_picks
的内容,因为我甚至无法触发它。
我希望我在这里做一些事情,因为我在我的智慧结束。谢谢你的期待。
修改1
按下提交按钮时传递的参数是:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EIEFdhjkkfnkjfYjlkjdlkjCzxaxHeaXbkvu0wbLB1QsOj+o=", "match_picks"=>{"1"=>{"id"=>"1", "result_pick"=>"6"}, "2"=>{"id"=>"2", "result_pick"=>""}, "3"=>{"id"=>"3", "result_pick"=>""}, "4"=>{"id"=>"4", "result_pick"=>""}, "5"=>{"id"=>"5", "result_pick"=>""}, "6"=>{"id"=>"6", "result_pick"=>""}, "7"=>{"id"=>"7", "result_pick"=>""}, "8"=>{"id"=>"8", "result_pick"=>""}, "9"=>{"id"=>"9", "result_pick"=>""}, "10"=>{"id"=>"10", "result_pick"=>""}, "11"=>{"id"=>"11", "result_pick"=>""}, "12"=>{"id"=>"12", "result_pick"=>""}, "13"=>{"id"=>"13", "result_pick"=>""}, "14"=>{"id"=>"14", "result_pick"=>""}, "15"=>{"id"=>"15", "result_pick"=>""}, "16"=>{"id"=>"16", "result_pick"=>""}, "17"=>{"id"=>"17", "result_pick"=>""}, "18"=>{"id"=>"18", "result_pick"=>""}, "19"=>{"id"=>"19", "result_pick"=>""}, "20"=>{"id"=>"20", "result_pick"=>""}, "21"=>{"id"=>"21", "result_pick"=>""}, "22"=>{"id"=>"22", "result_pick"=>""}, "23"=>{"id"=>"23", "result_pick"=>""}, "24"=>{"id"=>"24", "result_pick"=>""}, "25"=>{"id"=>"25", "result_pick"=>""}, "26"=>{"id"=>"26", "result_pick"=>""}, "27"=>{"id"=>"27", "result_pick"=>""}, "28"=>{"id"=>"28", "result_pick"=>""}, "29"=>{"id"=>"29", "result_pick"=>""}, "30"=>{"id"=>"30", "result_pick"=>""}, "31"=>{"id"=>"31", "result_pick"=>""}, "32"=>{"id"=>"32", "result_pick"=>""}, "33"=>{"id"=>"33", "result_pick"=>""}, "34"=>{"id"=>"34", "result_pick"=>""}, "35"=>{"id"=>"35", "result_pick"=>""}, "36"=>{"id"=>"36", "result_pick"=>""}, "37"=>{"id"=>"37", "result_pick"=>""}, "38"=>{"id"=>"38", "result_pick"=>""}, "39"=>{"id"=>"39", "result_pick"=>""}, "40"=>{"id"=>"40", "result_pick"=>""}, "41"=>{"id"=>"41", "result_pick"=>""}, "42"=>{"id"=>"42", "result_pick"=>""}, "43"=>{"id"=>"43", "result_pick"=>""}, "44"=>{"id"=>"44", "result_pick"=>""}, "45"=>{"id"=>"45", "result_pick"=>""}, "46"=>{"id"=>"46", "result_pick"=>""}, "47"=>{"id"=>"47", "result_pick"=>""}, "48"=>{"id"=>"48", "result_pick"=>""}}, "button"=>""}
编辑2(求助)
我意识到我使用的是form_for
而不是form_tag
。当我将其更改为form_tag
我的自定义路线触发时。傻错!!谢谢你看
答案 0 :(得分:1)
您应该使用form_tag而不是form_for,因为您在此处没有使用表单中的任何模型对象。
答案 1 :(得分:0)
切换
params.require(:match_pick)
到
params.require(:match_picks)