简体   繁体   English

如何使用用户定义的字段数制作表单? - 导轨上的 Ruby

[英]How can I make a form with a user defined number of fields? - Ruby on Rails

I'm making a ruby on rails app with a settings page where a user can enter two fields, a name and value field which will be translated upon form submit to the key and value of a hash.我正在使用设置页面在 Rails 应用程序上制作 ruby,用户可以在其中输入两个字段,一个名称和值字段,将在表单提交时转换为 hash 的键和值。

The only complicated aspect is that the user needs to be able to add additional "rows" to this form.唯一复杂的方面是用户需要能够向此表单添加额外的“行”。 The form on the page currently looks like this:当前页面上的表单如下所示:

在此处输入图像描述

So as you can see, clicking on the 'plus' icon will create another 'Conversion Action' and clicking on the 'minus' icon will remove a row from the field.如您所见,单击“加号”图标将创建另一个“转换操作”,单击“减号”图标将从字段中删除一行。 The user should be able to add as many Conversion Actions as they wish.用户应该能够根据需要添加任意数量的转化操作。

It's pretty standard to load this hash from the database and populate the form with existing values, but I'm not satisfied with my current way of adding additional form fields to the form.从数据库加载此 hash 并使用现有值填充表单是非常标准的,但我对当前向表单添加其他表单字段的方式不满意。

This is the code for the HTML form in use on the page:这是页面上使用的 HTML 表格的代码:

<%= parent_form.fields_for :conversion_trackers_to_pull do |form| %>
  <div id="conversion-trackers-to-pull">
    <% section.conversion_trackers_to_pull.each_with_index do |(conversion_tracker_name, conversion_tracker_id), index| %>
      <div class="mb-2">
        <%= form.fields_for "conversion_tracker_#{index}".to_sym do |conversion_tracker| %>
          <%= conversion_tracker.label :conversion_action, "Conversion Action #{index+1}: " %>
          <%= conversion_tracker.text_field :name, value: conversion_tracker_name, placeholder: "Name" %>
          <%= conversion_tracker.text_field :id, value: conversion_tracker_id, placeholder: "ID" %>
        <% end %>
        <%= button_tag '<i class="fas fa-minus-circle"></i>'.html_safe, type: "button", class: "remove-conversion-tracker" %>
        <span class="index d-none"><%= index + 1 %></span>
      </div>
    <% end %>
  </div>
  <i class="far fa-plus-square" id="<%= "#{section_name}" %>-new-conversion-tracker"></i>
<% end %>

And this is the JQuery code being used on the plus button click event:这是加号按钮单击事件中使用的 JQuery 代码:

$('#search-new-conversion-tracker').on('click', function() {
      var index = parseFloat(($('#search-conversion-trackers-to-pull').find('.index').last().text()));
      if (!index) {
        index = 0;
      }
      $(this).prev('#conversion-trackers-to-pull').append(
       '<div class="mb-2">' +
          '<label for="_search_section_config_conversion_trackers_to_pull_conversion_tracker_' + index + '_conversion_action">Conversion Action ' + (index+1) + ': </label>&nbsp;' +
          '<input type="text" name="[search_section_config][conversion_trackers_to_pull][conversion_tracker_' + index + '][name]" id="_search_section_config_conversion_trackers_to_pull_conversion_tracker_' + index + '_name" placeholder="Name">&nbsp;' +
          '<input type="text" name="[search_section_config][conversion_trackers_to_pull][conversion_tracker_' + index + '][id]" id="_search_section_config_conversion_trackers_to_pull_conversion_tracker_' + index + '_id" placeholder="ID">&nbsp;' +
          '<button name="button" type="button" class="remove-conversion-tracker"><i class="fas fa-minus-circle"></i></button>' +
          '<span class="index d-none">' + (index + 1) + '</span>' +
       '</div>'
      );
      bindRemoveConversionTracker();
    });

So this solution does work, albeit it's very clunky.所以这个解决方案确实有效,尽管它非常笨重。 I know a rails helper would probably be doing this exact same thing under the hood, but I need to replicate a similar type of form on other pages and I feel like there should be a cleaner way than to just repeat the code I've already got.我知道 Rails 助手可能会在后台做同样的事情,但我需要在其他页面上复制类似类型的表单,我觉得应该有一种更简洁的方法,而不是重复我已经写过的代码得到。

Does anyone have experience making this kind of form in Ruby on Rails?有没有人有在 Ruby on Rails 中制作这种表格的经验? What technique did you use?你用了什么技术? Thanks!谢谢!

To replicate a similar type of form on other pages, use partials .要在其他页面上复制类似类型的表单,请使用partials Keep what ever you feel redundant in the partial file and use it whenever you want.将您觉得多余的内容保留在部分文件中,并随时使用。 For better understanding of partials follow official documentation https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials为了更好地理解部分,请遵循官方文档https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:我如何允许用户在表单上设置文本样式? - Ruby on Rails: How can I allow a user to style text on a form? 如何在Ruby On Rails中自动完成表单字段 - How to complete form fields automatically in Ruby On Rails 如何在Rails中将红宝石动态地添加到表单(一键单击2个)到表单中 - How do I dynamically add form fields (2 in one click) to my form in ruby on rails 如何在 Ruby On Rails 中制作下拉表单 - How to make a dropdown form work in Ruby On Rails 使用 Braintree 的托管字段加载表单时,如何使浏览器专注于长信用卡号字段 - How can I make browser focus on the long credit card number field when loading a form using Braintree's Hosted Fields 如何创建一个按钮以清除表单中的一个字段? - How can i make a button to clear one of my fields in a form? 如何使 onClick 应用于数字表单的标签? - How can I make onClick apply to the label of a number form? 我怎样才能使它需要用户点击一个按钮,要验证的形式? - How can I make it necessary for the user to click a button for the form to be validated? Ruby On Rails:引用同一对象的表单字段 - Ruby On Rails: Form fields that refer to the same object 如何在Rails中执行AJAX-y条件显示/隐藏表单字段? - How can I do AJAX-y conditional show/hide form fields in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM