动态添加&任意次数地删除表单中相同的新字段
Dynamically add & remove new but the same fields of a form any amount times
我有一个没有嵌套模型的表单。
class UserProductsController
def new
@user_product = current_user.user_products.build
end
end
与jquery/javascript我想添加和删除新的@user_product字段在同一形式。正常情况下是这样的:
<%= form_for(@user_product) do |f| %>
<%= f.error_messages %>
<%= f.text_field :product %>
<%= f.text_field :address %>
<%= f.text_field :price %>
<%= f.submit %>
<% end %>
And I'm trying to achieve:
<%= form_for(@user_product) do |f| %>
<%= f.error_messages %>
<%= f.text_field :product_name %>
<%= f.text_field :address %>
<%= f.text_field :price %>
---New ID(New generated ID, could be any new number in the database)
<%= f.text_field :product_name %>
<%= f.text_field :address %>
<%= f.text_field :price %>
<%= Remove this Product %> # ajax style, this link removes this product entirely
----ID 3----
<%= f.text_field :product_name %>
<%= f.text_field :address %>
<%= f.text_field :price %>
<%= Remove this Product %>
-----Add link------
<%= Add Another Field %> # ajax style adding
<%= f.submit %>
<% end %>
请注意,新id没有预先设置,还有一个提交按钮,这样用户就可以一次创建多个Product。我如何实现上述目标?
谢谢。
答:
使用用户。你可以照詹姆斯说的去做。这是我的表格。
<%= form_for(current_user, :url => user_products_path) do |f| %>
<%= f.error_messages %>
<%= f.fields_for :user_products do |builder| %>
<%= render "user_product_fields", :f => builder %>
<% end %>
<%= link_to_add_fields "Add a UserProduct", f, :user_products %>
<%= f.submit "Create" %>
<% end %>
_user_product_fields.html.erb
<div class="fields">
<%= f.label :product, "Product" %>
<%= f.text_field :product %>
<%= f.label :address, "Address" %>
<%= f.text_field :address %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= link_to_remove_fields "remove", f %>
</div>
就像其他人建议的那样(有些没用),使用嵌套的表单。
您需要表单基于current_user对象,并为:user_products声明fields_for。你需要使用url_for将表单的url设置为发送回user_product控制器,而不是用户控制器。你还需要调整user_product控制器的更新和创建操作,以使用params[:user]而不是params[:user_product]
如果你有你的accepts_nested_attributes配置正确,所有应该是好的。
顺便说一句,如果你遵循这些railcast,你会发现添加新的javascript将无法在Rails 3.x中工作
你应该在你的视图帮助器
中使用这样的东西module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')")
end
end
之类的东西在application。js文件
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).up().insert({
after: content.replace(regexp, new_id)
});
}
显然你的form_for需要被重构成这样的东西
<%= form_for(current_user, :url => user_product_path) do |f| %>
<!-- add your f.fields_for :user_products here, probably in a rendered partial -->
<%end%>
这应该足以让您使用rails强制转换
Checkout this:
http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-form-part-2
可以使用cocoon
相关文章:
- 从按钮生成新的输入字段
- 每次按下按钮时添加新的文本字段
- 如何使用jQuery添加输入字段并将Ajax调用到新的输入字段中
- jQuery 选择错误:添加新的选择字段并对其进行组合时出现问题
- 在方法中动态声明新的“类字段” - 是否是好的做法
- 填写所有先前字段后,添加新的输入字段
- 使用 JQuery 在不涉及整页刷新的动态页面更改后访问/传播选择器到新的输入字段
- 如何在“”上动态地添加新的输入字段;keydown”;使用jQuery
- HTML表单可访问性:选项卡键焦点跳过新显示的字段
- 如何使用.keyup()添加新的输入字段
- 在新窗口中打开Instagram照片页面,向下滚动页面/关注新的评论字段
- JS:用新日期填充字段,从用户输入中减去x天
- 如何使用REST API用新值更新字段
- 在下拉菜单中选择选项后,添加新的输入字段
- JavaScript添加新的输入字段点击
- JavaScript在点击时生成新的输入字段
- Jquery不会将listener添加到新添加的字段中
- 如何从输入字段捕获文本值,并将其传递给新的输入字段
- 用AngularJS的方式给MongoLab添加一个新的模型字段
- 按递增顺序添加新的输入字段