English 中文(简体)
铁路集团在提交表格时有多阵s?
原标题:How does Rails group arrays of hashes during form submission?

According to the Rails Guides and this Railscasts episode, when there s a one-to-many association between two objects (e.g. Project and Task), we can submit multiple instances of Task together with the Project during form submission similar to this:

<% form_for :project, :url => projects_path do |f| %>
  <p>
    Name: <%= f.text_field :name %>
  </p>
  <% for task in @project.tasks %>
    <% fields_for "project[task_attributes][]", task do |task_form| %>
      <p>
        Task Name: <%= task_form.text_field :name %>
    Task Duration: <%= task_form.text_field :duration %>
      </p>
    <% end %>
  <% end %>
  <p><%= submit_tag "Create Project" %></p>
<% end %>

This will result multiple copies of an HTML block like this in the form, one for each task:

<p>
    Task Name: <input name="project[task_attributes][name]">
    Task Duration: <input name="project[task_attributes][duration]">
</p>

My question is, how does Rails understand which

    (project[task_attributes][name], project[task_attributes][duration])

belong together, and packing them into a hash element of the resulting array in params? Is it guaranteed that the browsers must send the form parameters in the same order in which they appear in the source?

问题回答

是的,定购单作为人保留,因为“k-everest Self-answered”是对原始问题的评论。

Those asking for HTML, see the guide on how the attribute s name is parsed.

Example of typically bad ordering:

cart[items][][id]=5
cart[items][][id]=6
cart[items][][name]=i1
cart[items][][name]=i2

而铁路则将铁路划入:

{ "cart"=> {"items"=> [
                        {"id"=>"5"},
                        {"id"=>"6", "name"=>"i1"},
                        {"name"=>"i2"}
                       ]}}

例源:https://spin.atomicobject.com/07/11/get-and-post-paraile-parsing-in-rails-2/

该特征在铁路初期投入使用,方法名称为build_deep_hash。 欲了解更多的历史,ski起火/se,并自此结束起担任最后职务:

如果你用直截了当的数据进行工作,并希望在不使用任何这些@objects的情况下再发送一个阵列。

<%= form_for :team do |t| %>
  <%= t.fields_for  people[] , [] do |p| %>
    First Name: <%= p.text_field :first_name %>
    Last Name: <%= p.text_field :last_name %>
  <% end %>
<% end %>

您的寄生虫数据应如上。

"team" => {
  "people" => [
    {"first_name" => "Michael", "last_name" => "Jordan"},
    {"first_name" => "Steve", "last_name" => "Jobs"},
    {"first_name" => "Barack", "last_name" => "Obama"}
  ]
}




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签