简体   繁体   English

Rails:从表单向服务器发送一系列哈希

[英]Rails: sending an array of hashes from form to server

In my parameters hash, I am trying to achieve something like the following: 在参数哈希中,我正在尝试实现以下目标:

{"utf8"=>"✓", "authenticity_token"=>"6A14fVUEWQSB83j2BsYKn", "search"=>{"keywords"=>[{"keyword_names"=>"color", "keyword_conditions"=>"is not", "keyword_values"=>"red"}, {"keyword_names"=>"size", "keyword_conditions"=>"is", "keyword_values"=>"3"}] , "assignee_id"=>"", "begin_date"=>"", "end_date"=>""}, "commit"=>"search"}

Notice the following: 请注意以下几点:

"keywords"=>[{"keyword_names"=>"color", "keyword_conditions"=>"is not", "keyword_values"=>"red"}, {"keyword_names"=>"size", "keyword_conditions"=>"is", "keyword_values"=>"3"}]

However, this is what is currently being sent: 但是,这是当前正在发送的内容:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"6A14fVUEWQSB83j2BsYKn", "keyword_names"=>["name", "name"], "keyword_conditions"=>["is", "is"], "keyword_values"=>["dan", "john"], "search_add"=>"and", "search"=>{"assignee_id"=>"", "begin_date"=>"", "end_date"=>""}, "commit"=>"search"}

Notice keyword_names, keyword_conditions, and keyword_values is not part of the search hash. 请注意,关键字名称,关键字条件和关键字值不属于搜索哈希。 How can I make it part of the search hash? 如何使其成为搜索哈希的一部分?

This is what I have: 这就是我所拥有的:

  <%= link_to "Add", "#", class: 'add_search'  %>
  <div class="search_container">
    <%= text_field_tag "keyword_names[]" %><%= select_tag "keyword_conditions[]", options_for_select(["is", "is not","is like"]) %><%= text_field_tag "keyword_values[]" %>
  </div>

Following this structure of the params: 遵循以下参数结构:

keywords: 
  [
    { keyword_names: 'color', keyword_conditions: 'is not', keyword_values: 'red' }
  ]

You should name your input like this: 您应该这样命名您的输入:

0..3.each do |index|
  text_field_tag "keywords[#{index}][keyword_names]"
  select_tag "keywords[#{index}][keyword_conditions]", options_for_select(['is', 'is not', 'is like'])
  text_field_tag "keywords[#{index}][keyword_values]"
end

Where the 0..3.each do |index| 0..3.each do |index| is a loop defining 4 different sets of 3 inputs for each keyword. 是一个循环,为每个关键字定义4组不同的3个输入。 So params[:keywords].size should return 4 所以params[:keywords].size应该返回4

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

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