如何删除,“删除”如果表单字段是带有 Nested_Form gem 的页面上的第一个表单字段,请链接?
我正在使用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%>
I am using the nested_form gem that allows the user to add and remove form fields dynamically. It is working fine, but I would like to be able to prevent the user from removing the first file_field, and only show the "Remove" link if it is a field that the user had added. My '_form' and '_photo_fields' partials are below. I think the solution would be to do something like, "if this field is not the first field of its type on the page, then, show the 'Remove' link," but I am not sure how to accomplish that. Thank you for any insight you can provide.
_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%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在 ERB 视图中隐藏它,因为 fields_for 块内呈现的内容将用作“添加新”按钮的模板。因此,如果您在那里隐藏删除链接,则添加的任何一项都不会具有删除链接。
当我查看nested_form_for代码时,它对此没有本机支持。最简单的解决方案,但并不理想,是通过 CSS 或 JS 隐藏客户端上的删除链接。
我用这个JS解决了这个问题:
然后添加'data-nested-form-disable-first'来删除链接定义:
它仅在您确保加载时不可删除的字段位于页面中时才有效。
you cannot hide it from ERB in view, beacause what is rendered inside the fields_for block is then used as template for the "add new" button. So if you hide remove link there, no one added item will have remove link.
As I looked on the nested_form_for code, it has no native support for this. Simplest solution, but not ideal, is to hide remove link on the client by CSS or JS.
I solved it with this JS:
And then add 'data-nested-form-disable-first' to remove link definition:
It works only when you have ensured that the not-removable field is in the page when it is loaded.
我在这里看到两个选项供您选择:
照片 API
尝试通过
_photo_fields.html.erb:
使用 CSS
等方法扩展您的照片模型,我不确定,但应该可以通过以下方式实现:
div
为您的照片字段所以这里是代码(只有您应该更改或添加的片段:
_photo_fields.html.erb:
application.css:
第一个解决方案是正确的,如果可能的话,因为它根本不允许删除不可删除的东西。第二个是隐藏链接的解决方法,但要弱得多。
I see here two options for you:
Photo API
Try to expand your photo model by a method like
_photo_fields.html.erb:
Using CSS
I'm not sure, but it should be possible by:
div
for your photo fieldSo here is the code (only the fragments you should change or add:
_photo_fields.html.erb:
application.css:
The first solution is the right one, if possible, because it does not allow at all to remove the non-removable thing. The second one is a workaround that hides the link, but is much weaker.