Rails:为表单中的数组元素赋值

时间:2013-06-03 18:53:23

标签: ruby-on-rails-3.2

我有一个用于上传用户照片的嵌套表单。

          <%= f.fields_for :photos do |photo| %>
                 <%= image_tag photo.object.avatar.url(:thumb), :class => "foodio_photo" unless f.object.new_record? or photo.object.new_record? %>
                 <%= photo.file_field :avatar, :style => "none" %>
          <% end %>
           <%= f.link_to_add 'Add Photos', :photos,
                     "data-association-insertion-node"=> ".photos",
                     "data-association-insertion-position" => "append",
                     "data-type" => "link", :class => "subcatlink"%> 

使用这个我可以上传多张照片,可以访问第一张,第二张等照片。但在这里我想上传为photos.first和photo.second而不是访问。

类似的东西:

          <%= f.fields_for :photos.first do |photo| %>
                 <%= image_tag photo.object.avatar.url(:thumb), :class => "foodio_photo" unless f.object.new_record? or photo.object.new_record? %>
                 <%= photo.file_field :avatar, :style => "none" %>
          <% end %>
          <%= f.fields_for :photos.second do |photo| %>
                 <%= image_tag photo.object.avatar.url(:thumb), :class => "foodio_photo" unless f.object.new_record? or photo.object.new_record? %>
                 <%= photo.file_field :avatar, :style => "none" %>
          <% end %>

但它不起作用。给出错误

  undefined method `new_record?' for nil:NilClass

有人可以告诉你怎么做吗?

0 个答案:

没有答案