Ruby on rails 在Rails中动态创建和删除多个文本字段?
我正在使用“嵌套表单”gem构建我的复杂Rails表单 在这种情况下,我需要删除或添加多行,如:Ruby on rails 在Rails中动态创建和删除多个文本字段?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,我正在使用“嵌套表单”gem构建我的复杂Rails表单 在这种情况下,我需要删除或添加多行,如: 1. name textbox purpose textbox description textarea 2. name textbox purpose textbox description textarea . . and so on.. 我可以一次删除或添加一个名称字段,但我需要同时创建上述三个字段。 我也通过了铁路197号,但没什么帮助 提前感谢。您可以使用Rails的嵌套属性,而
1. name textbox purpose textbox description textarea
2. name textbox purpose textbox description textarea
.
.
and so on..
我可以一次删除或添加一个名称字段,但我需要同时创建上述三个字段。
我也通过了铁路197号,但没什么帮助
提前感谢。您可以使用Rails的嵌套属性,而不是使用任何gem 假设您有一个名为
公司
的模型,其中公司可以有许多员工:
class Company < ActiveRecord::Base
has_many :employees
accepts_nested_attributes_for :employees
end
class公司
您的公司表格将类似于:
<%= form_for(@company, :html => {:class => "form-horizontal"}) do |f| %>
<a href="#">Add Employee</a>
<div class="employees"></div> <!-- Append the HTML template for employees (controller with JS) -->
<% end %>
<div class="field more_cancer_types" class="field">
<%= hidden_field_tag "company[employees_attributes][][id]", nil %>
<%= text_field_tag "company[employees_attributes][][name]", nil, :placeholder=> " - Add Employee - " %>
<a class="remove_field" href="#"><i class="icon-trash"></i> Remove</a>
</div>
{:class=>“form horizontal”})do | f |%>
员工的HTML模板如下所示:
<%= form_for(@company, :html => {:class => "form-horizontal"}) do |f| %>
<a href="#">Add Employee</a>
<div class="employees"></div> <!-- Append the HTML template for employees (controller with JS) -->
<% end %>
<div class="field more_cancer_types" class="field">
<%= hidden_field_tag "company[employees_attributes][][id]", nil %>
<%= text_field_tag "company[employees_attributes][][name]", nil, :placeholder=> " - Add Employee - " %>
<a class="remove_field" href="#"><i class="icon-trash"></i> Remove</a>
</div>
“-添加员工-”%>
而且,使用JavaScript,您可以添加或删除此模板。不用担心rails会处理它(将准备一个包含每个实体的employees属性数组),如果未选中复选框,请确保通过
nil
。您能够实现吗?