简体   繁体   English

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

[英]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.

相关问题 Rails 6 使用 has_many 关系和 accepts_nested_attributes_for 连接表 - Rails 6 Joins Tables using has_many relationships and accepts_nested_attributes_for 使用accepts_nested_attributes_for在rails中的三层嵌套表单 - Three level nested forms in rails using accepts_nested_attributes_for Rails 问题 accept_nested_attributes_for - Rails problem accepts_nested_attributes_for Rails和JQuery:无效的关联。 确保将accepts_nested_attributes_for用于 - Rails & JQuery: Invalid association. Make sure that accepts_nested_attributes_for is used for Rails嵌套属性form_for for has_many,使用javascript无限制嵌套模型 - Rails Nested Attributes form_for for has_many with unlimited nested models using javascript Nested_form_fields在验证失败时显示现有的has_many嵌套字段 - Nested_form_fields show existing has_many nested fields upon validation failure Rails通过连接记录通过JavaScript将新的has_many附加到嵌套表单 - Rails append new has_many through join record to nested form via javascript 如何在没有nested_form的情况下将表单写入has_many关联? - How to write form to has_many association without nested_form? 如何防止(jquery)ajax调用中的许多嵌套函数? - How to prevent many nested functions within an (jquery) ajax call? jQuery Ajax嵌套回调 - jquery ajax nested callbacks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM