0

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

          <%= 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

谁能告诉我该怎么做?

4

0 回答 0