有人能解释一下下面的代码示例吗?"albumphoto_attributes“是什么意思?我在这里找到了这段代码http://infrastacks.com/?p=57
<div class="photo">
<% fields_for "album[photo_attributes][]", photo do |p| %>
<p>
<%= p.label :Photo %><br />
<%= p.file_field :data, :index => nil %>
<%= link_to_function "delete", "remove_field($(this), ('.photo'))" %>
</p>
<% end %>
</div>发布于 2010-06-05 06:54:47
从字面上看,它是一种结构,它告诉rails将所有提交分组到一个哈希表中,以便您可以一次遍历一个提交。
因此,在这种情况下,哈希表‘相册’是双重索引的。通过不将第二个项目的显式索引号放在哈希表中(由photo_attributes后面的左括号和闭括号表示),rails知道将具有该哈希名(相册)和第一个索引值(照片属性)的所有提交连接到一个哈希表中,其中与对象关联的photo_attributes是一个数组。该数组中的每个条目都是一个散列,其索引处有一个值:data。
## From the code on that page
params[:album][:photo_attributes]
#This turns out to be an array of hashes. Each hash has one key/value pair in it. The key is "data" and the value is the file information. Example:
{"data"=>#<File:/var/folders/56/56dUsTxtHaKheeiHSoaE1++++TI/-Tmp-/CGI20081216-17582-14p6wd2-0>}
params[:album][:photo_attributes].each { |p| p[:data] } # this is a loop that would get you the data for each photo submitted.https://stackoverflow.com/questions/2978079
复制相似问题