我有一个没有嵌套模型的表单。
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 %>
我正在努力实现:
<%= 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 和一个提交按钮,这样用户一次可以创建多个产品。我如何实现上述目标?
谢谢。
回答:
使用 User.rb 模型你可以做 James 所说的。这是我有效的表格。
<%= 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 以使用 url_for 回发到 user_product Controller 而不是用户 Controller ,并且您将需要调整 user_product Controller 更新并创建操作以使用 params[:user] 而不是 params[:用户产品]
如果您的 accepts_nested_attributes 配置正确,一切都应该是好的。
顺便说一句,如果你关注那些 railscasts,你会发现添加新的 javascript 不适用于 Rails 3.x
你应该为你的 View 助手使用这样的东西
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 文件中为您的 javascript 编写类似的内容
// 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 cast 来说应该足够了
关于javascript - 任意次数地动态添加和删除新的但相同的表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057215/