我正在尝试创建一个嵌套表单,其中有一个“+”按钮,通过jquery和ajax添加字段。我正在做它一次添加一个步骤,所以直到现在我能够添加一个简单的文本按“+”按钮,但是,正如我刚才所说,我想添加一些字段。
我有以下几点:
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 %>
产品/ _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
class CampaignsController < ApplicationController
....
def add_good_row
respond_to do |format|
format.js
end
end
end
add_good_row.js.erb
$('.goods').append("hello");
我认为我理解了这个问题,就是将f
变量,@campaign
变量和child_index
传递给部分,但我怎么能通过jquery做到这一点?
希望我很清楚...