如何删除,“删除”如果表单字段是带有 Nested_Form gem 的页面上的第一个表单字段,请链接?

发布于 2024-12-20 19:57:10 字数 1089 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我早已燃尽 2024-12-27 19:57:10

您无法在 ERB 视图中隐藏它,因为 fields_for 块内呈现的内容将用作“添加新”按钮的模板。因此,如果您在那里隐藏删除链接,则添加的任何一项都不会具有删除链接。

当我查看nested_form_for代码时,它对此没有本机支持。最简单的解决方案,但并不理想,是通过 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();
    }
  });
});

然后添加'data-nested-form-disable-first'来删除链接定义:

<%= f.link_to_remove 'Remove', '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:

$(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();
    }
  });
});

And then add 'data-nested-form-disable-first' to remove link definition:

<%= f.link_to_remove 'Remove', 'data-nested-form-disable-first' => '' %>

It works only when you have ensured that the not-removable field is in the page when it is loaded.

携余温的黄昏 2024-12-27 19:57:10

我在这里看到两个选项供您选择:

  1. 如果您的照片被询问是否可以删除,那将是最简单的解决方案
  2. 您可以尝试通过 CSS 隐藏表单中的链接。

照片 API

尝试通过

class Photo ...
  def removable?
    ...
  end
end

_photo_fields.html.erb:

...
<%= f.file_field :photo %>
<% if f.object.photo.removable? %>
  <%= f.link_to_remove "Remove" %>
<% end %>

使用 CSS

等方法扩展您的照片模型,我不确定,但应该可以通过以下方式实现:

  1. 添加 div为您的照片字段
  2. 添加相关规则到您的 CSS 中。

所以这里是代码(只有您应该更改或添加的片段:

_photo_fields.html.erb:

<div class="photo_fields">
   ...
   <%= f.link_to_remove "Remove", :class => "remove" %>
   ...
</div>

application.css:

div.photo_field:first-child > a.remove {
  display: none;
}

第一个解决方案是正确的,如果可能的话,因为它根本不允许删除不可删除的东西。第二个是隐藏链接的解决方法,但要弱得多。

I see here two options for you:

  1. If your photo could be asked if it can be removed, that would be the easiest solution
  2. You could try to hide the link in the form by CSS.

Photo API

Try to expand your photo model by a method like

class Photo ...
  def removable?
    ...
  end
end

_photo_fields.html.erb:

...
<%= f.file_field :photo %>
<% if f.object.photo.removable? %>
  <%= f.link_to_remove "Remove" %>
<% end %>

Using CSS

I'm not sure, but it should be possible by:

  1. Add a div for your photo field
  2. Add to your CSS the relevant rules.

So here is the code (only the fragments you should change or add:

_photo_fields.html.erb:

<div class="photo_fields">
   ...
   <%= f.link_to_remove "Remove", :class => "remove" %>
   ...
</div>

application.css:

div.photo_field:first-child > a.remove {
  display: none;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文