0
我想創建一個嵌套的窗體,其中有一個「+」按鈕,通過jQuery和ajax添加字段。我現在正在添加一個步驟,現在我可以添加一個簡單的文本來推動「+」按鈕,但正如我剛纔所說,我想添加一些字段。has_many和accepted_nested_attributes_for與嵌套窗體和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][email protected], 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的?
希望我很清楚...
如果我理解正確你的問題,這2部分[事件](http://railscasts.com/episodes/196-nested-model-form-part- 1)的RailsCast正是你正在尋找的。 – AbM