0

我正在传递一个带有表单参数的数组来处理。它包含几行,每行都有多个值。如何检索单个值?这是控制器代码:

params[:line].each do |line|
  ... 
end

每条“线”都给了我这些值,我想检索每个值,比如“home_score_id”:

["1", {":home_club_id"=>"11", ":home_score"=>"2", ":away_score"=>"4", ":away_club_id"=>"10"}]

更新:

这是表格,它有 12 行供您输入匹配结果:

= form_tag store_all_results_path, class: 'form-horizontal' do
  .control-group
    .controls
      = select_tag :gameround_id, options_for_select(@gamerounds.map{ |g| ["#{markup_gameround(g)}", g.id] })
  - 1.upto(12) do |counter|
    .control-group
      .controls
      = select_tag "line[#{counter}][:home_club_id]", options_for_select(@clubs.map{ |c| [c.club_name, c.id] }), include_blank: true
      = text_field_tag "line[#{counter}][:home_score]", nil, class: "small_text_field"
      = text_field_tag "line[#{counter}][:away_score]", nil, class: "small_text_field"
      = select_tag "line[#{counter}][:away_club_id]", options_for_select(@clubs.map{ |c| [c.club_name, c.id] }), include_blank: true
  .form-actions
    = submit_tag :submit, value: I18n.t('.general.save'), class: "btn"
    = link_to t('.cancel', default: t("helpers.links.cancel")), results_path, class: 'btn'

提交时,它会提供以下 POST 参数:

Started POST "/results/store_all" for 127.0.0.1 at 2013-04-03 10:47:34 +0200
Processing by ResultsController#store_all as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "gameround_id"=>"3", "line"=>{"1"=>{":home_club_id"=>"8", ":home_score"=>"3", ":away_score"=>"2", ":away_club_id"=>"10"}, "2"=>{":home_club_id"=>"7", ":home_score"=>"4", ":away_score"=>"2", ":away_club_id"=>"12"}, "3"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "4"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "5"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "6"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "7"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "8"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "9"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "10"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "11"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}, "12"=>{":home_club_id"=>"", ":home_score"=>"", ":away_score"=>"", ":away_club_id"=>""}}, "commit"=>"Opslaan"}

在控制器中,我想遍历 12 行中的每一行并创建一个结果记录(当行不为空时):

 def store_all
    params[:line].each do |line|
      unless line[:home_club_id].blank?
        Result.create(home_club_id: line[:home_club_id], away_club_id: line[:away_club_id],  home_score: line[:home_score], away_score: line[:away_score], gameround_id: params[:gameround_id])
      end
    end
  end
4

2 回答 2

0

"line" 中的第二个值是 a Hash,因此可以使用

line[1][:home_club_id]
于 2013-04-03T06:58:29.177 回答
0

尽管您的键看起来像符号 ( :home_club_id),但它们实际上是以冒号 ( ":home_club_id") 为前缀的字符串。从您的视图中删除冒号应该可以解决问题。

所以而不是:

= text_field_tag "line[#{counter}][:away_score]"

写吧:

= text_field_tag "line[#{counter}][away_score]"

由于paramsHashWithIndifferentAccess,您可以在其中一个line["away_score"]line[:away_score]在您的控制器中使用。

def store_all
  params[:line].each do |counter, line|
    unless line[:home_club_id].blank?
      Result.create(line.merge(gameround_id: params[:gameround_id])
    end
  end
end
于 2013-04-03T09:10:50.787 回答