Ruby on rails “如何删除”;删除“;如果表单字段是页面上具有嵌套表单gem的第一个表单字段,则链接?
我正在使用嵌套的表单gem,它允许用户动态添加和删除表单字段。它工作正常,但我希望能够阻止用户删除第一个file_字段,并且仅当该字段是用户添加的字段时才显示“Remove”链接。下面是我的“\u表单”和“\u照片\u字段”部分。我认为解决办法是这样做,“如果这个字段不是页面上它类型的第一个字段,那么,显示'Remove'链接”,但我不确定如何做到这一点。感谢您提供的任何见解 \u form.html.erb:Ruby on rails “如何删除”;删除“;如果表单字段是页面上具有嵌套表单gem的第一个表单字段,则链接?,ruby-on-rails,ruby-on-rails-3,forms,nested-form-for,Ruby On Rails,Ruby On Rails 3,Forms,Nested Form For,我正在使用嵌套的表单gem,它允许用户动态添加和删除表单字段。它工作正常,但我希望能够阻止用户删除第一个file_字段,并且仅当该字段是用户添加的字段时才显示“Remove”链接。下面是我的“\u表单”和“\u照片\u字段”部分。我认为解决办法是这样做,“如果这个字段不是页面上它类型的第一个字段,那么,显示'Remove'链接”,但我不确定如何做到这一点。感谢您提供的任何见解 \u form.html.erb: <%= simple_nested_form_for @service_off
<%= 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>
<% 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%>
...
<%= f.file_field :photo %>
<% if f.object.photo.removable? %>
<%= f.link_to_remove "Remove" %>
<% end %>
<div class="photo_fields">
...
<%= f.link_to_remove "Remove", :class => "remove" %>
...
</div>
div.photo_field:first-child > a.remove {
display: none;
}
... 其他领域。。。
建筑商%>
\u photo\u fields.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>
<% 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%>
...
<%= f.file_field :photo %>
<% if f.object.photo.removable? %>
<%= f.link_to_remove "Remove" %>
<% end %>
<div class="photo_fields">
...
<%= f.link_to_remove "Remove", :class => "remove" %>
...
</div>
div.photo_field:first-child > a.remove {
display: none;
}
#这就是我想放弃if语句的地方,但我不确定它应该是什么。
#
我在这里看到两个选项供您选择:
class Photo ...
def removable?
...
end
end
\u photo\u fields.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>
<% 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%>
...
<%= f.file_field :photo %>
<% if f.object.photo.removable? %>
<%= f.link_to_remove "Remove" %>
<% end %>
<div class="photo_fields">
...
<%= f.link_to_remove "Remove", :class => "remove" %>
...
</div>
div.photo_field:first-child > a.remove {
display: none;
}
application.css:
<%= 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>
<% 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%>
...
<%= f.file_field :photo %>
<% if f.object.photo.removable? %>
<%= f.link_to_remove "Remove" %>
<% end %>
<div class="photo_fields">
...
<%= f.link_to_remove "Remove", :class => "remove" %>
...
</div>
div.photo_field:first-child > a.remove {
display: none;
}
如果可能的话,第一种解决方案是正确的,因为它根本不允许移除不可移除的东西。第二个是隐藏链接的解决方法,但要弱得多。您无法在视图中对ERB隐藏链接,因为块的字段_内呈现的内容随后用作“添加新”按钮的模板。所以,若你们把删除链接隐藏在那个里,并没有一个添加的项目会有删除链接 当我查看嵌套的\u表单\u的代码时,它没有本机的支持。最简单但并不理想的解决方案是通过CSS或JS隐藏客户端上的删除链接 我用这个JS解决了这个问题:
$(function() {
$('a[data-nested-form-disable-first]').each(function() {
var nested_container = $(this).closest('.fields');
// check if is first
if (!nested_container.prev().is('.fields')) {
$(this).remove();
}
});
});
然后添加“数据嵌套表单先禁用”以删除链接定义:
<%= f.link_to_remove 'Remove', 'data-nested-form-disable-first' => '' %>
“”%>
只有当您确保加载页面时页面中有“不可移动”字段时,它才起作用。感谢您的回复!CSS方法是有道理的,但我对rails还是相当陌生,我很难弄清楚“removable?”方法可能是什么。你能提供什么帮助吗?我发现这是关于为“form_for”指定一个实例的,我不确定这是否有帮助?另外,你知道有什么好的资源可以用来学习写方法吗?我在谷歌上找不到任何东西。非常感谢。非常感谢。这对我帮助很大!很好的解决方案。