簡體   English   中英

has_many和accepts_nested_attributes_for具有嵌套表單和jquery / ajax

[英]has_many and accepts_nested_attributes_for with nested forms and jquery/ajax

我正在嘗試創建一個嵌套表單,其中有一個“ +”按鈕,可通過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 %>

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

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做到這一點呢?

希望我很清楚...

看看這個寶石。
我想這就是你要找的
https://github.com/nathanvda/cocoon

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM