[英]has_many and accepts_nested_attributes_for with nested forms and jquery/ajax
I'm trying to create a nested form where there is a "+" button which adds fields via jquery and ajax. 我正在尝试创建一个嵌套表单,其中有一个“ +”按钮,可通过jquery和ajax添加字段。 I am doing it adding a step at once, so untill now I am able to add a simple text pushing the "+" button, but, as I have just said, I want to add some fields. 我正在一次添加一个步骤,所以直到现在为止,我都可以按“ +”按钮添加一个简单的文本,但是,正如我刚才所说的,我想添加一些字段。
I have the followings: 我有以下几点:
new.html.erb new.html.erb
<h1>New Campaign</h1>
<%= form_for @campaign, html: { multipart: true } do |f| %>
....
<div class="goods">
<%= render "goods/good", f: f, child_index: Time.now.to_i %>
</div>
<div class="actions">
<%= link_to '+', add_good_row_path(params[:campaign]=@campaign, params[:f] = f), remote: true, class: 'btn btn-large btn-success' %>
<%= f.submit 'Crea', class: 'btn btn-large btn-warning' %>
</div>
<% end %>
goods/_good.html.erb goods / _good.html.erb
<%= f.fields_for :goods, @campaign.goods.build, child_index: child_index do |good| %>
<%= good.text_field :name, placeholder: 'Nome' %>
<%= good.text_area :description, placeholder: 'Descrizione' %>
<%= good.text_field :cost, placeholder: 'Costo' %>
<% end %>
campaigns_controller.rb campaigns_controller.rb
class CampaignsController < ApplicationController
....
def add_good_row
respond_to do |format|
format.js
end
end
end
add_good_row.js.erb add_good_row.js.erb
$('.goods').append("hello");
I think that I understood the problem, it is to pass a f
variable, a @campaign
variable and a child_index
to the partial, but how can I do that via jquery? 我认为我理解了问题,它是将f
变量, @campaign
变量和child_index
传递给局部变量,但是如何通过jquery做到这一点呢?
Hope I was clear... 希望我很清楚...
Have a look at this gem. 看看这个宝石。
I think it's what you are looking for 我想这就是你要找的
https://github.com/nathanvda/cocoon https://github.com/nathanvda/cocoon
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.