我正在使用nested_form gem,它允许用户动态添加和删除表单字段。它工作正常,但我希望能够阻止用户删除第一个 file_field,并且仅在用户添加的字段时才显示“删除”链接。我的“_form”和“_photo_fields”部分如下。我认为解决方案是执行类似“如果此字段不是页面上其类型的第一个字段,则显示‘删除’链接”之类的操作,但我不知道如何实现。感谢您提供的任何见解。
_form.html.erb:
<%= simple_nested_form_for @service_offering do |f|%>
... Other Fields ...
<%= f.fields_for :photos do |builder| %>
<%= render 'photo_fields', :f => builder %>
<%end%>
<%= f.button :submit %>
<p><%= f.link_to_add "Add Photo", :photos %></p>
_photo_fields.html.erb:
<% if f.object.new_record? %>
<%= f.label :photo %>
<%= f.file_field :photo %>
# This is where I want to drop that if statement, but I am not sure what it should be.
<%= f.link_to_remove "Remove" %>
#
<%end%>
<% unless f.object.new_record? %>
<%= link_to( image_tag(f.object.photo.url(:thumb))) %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
<%end%>