简体   繁体   English

Rails 4更新多个记录

[英]Rails 4 Update Multiple Records

Been at this the last 2 days and can't figure out what I'm doing wrong. 最近两天都在这里,无法弄清楚我在做什么错。 I'm trying to update multiple match_pick objects on a screen at the same time using form_tag . 我正在尝试使用form_tag同时更新屏幕上的多个match_pick对象。 Bare match_pick records get created as soon as a user signs up. 用户注册后, match_pick创建裸match_pick记录。 I then want the user to be able to update relevant info on these records and press a submit button. 然后,我希望用户能够更新这些记录上的相关信息,然后按提交按钮。 Here's what I have so far: 这是我到目前为止的内容:

routes.rb routes.rb

post "match_picks/adjust_match_picks" => "match_picks#adjust_match_picks", :as => :adjust_match_picks

match_picks_controller.rb 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

views/match_picks/index.html.erb views / 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 %>  

The HTML generated by the above is as follows:- 上面生成的HTML如下:

  <form accept-charset="UTF-8" action="/match_picks" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="EIEFdhjkkfnkjfYjlkjdlkjCzxaxHeaXbkvu0wbLB1QsOj+o=" /></div>

The above code is giving the following error: 上面的代码给出了以下错误:

    ActionController::ParameterMissing in MatchPicksController#create

    param is missing or the value is empty: match_pick

It then highlights this paragraph 然后突出显示此段

        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

I've put some debug in each of the methods in the match_picks controller and the form submit seems to be triggering the create method of the controller instead of my custom adjust_match_picks method. 我在match_picks控制器的每个方法中都进行了一些调试,表单提交似乎触发了控制器的create方法,而不是我的自定义adjust_match_picks方法。 It looks to me like the error is triggering because the create method is not being passed match_pick_params . 在我看来,由于未传递create方法match_pick_params因此触发了错误。 But I don't want the create method triggering, I want my custom adjust_match_picks to trigger. 但是我不想触发create方法,而是希望触发我的自定义adjust_match_picks

I haven't put any code in to handle what's being passed to the adjust_match_picks as yet because I can't even get it to trigger. 我还没有放入任何代码来处理传递给adjust_match_picks ,因为我什至无法触发它。

I'm hoping I'm doing something sill here as I'm at my wits end. 我希望自己在机智的时候在这里做点事。 Thanks for looking. 感谢您的光临。

Edit 1 编辑1

The parameters that are getting passed when the submit button is pressed are: 按下提交按钮时传递的参数是:

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

Edit 2 (SOLVED) 编辑2(已解决)

I've realised I was using a form_for instead of a form_tag . 我意识到我使用的是form_for而不是form_tag WHen I change it to form_tag my custom route fires. 当我将其更改为form_tag我的自定义路线火灾。 Silly mistake!! 愚蠢的错误! Thanks for looking 谢谢看

您应该使用form_tag而不是form_for,因为这里您没有在form中使用任何模型对象。

Switch 开关

params.require(:match_pick)

To

params.require(:match_picks)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM