简体   繁体   English

Rails通过连接记录通过JavaScript将新的has_many附加到嵌套表单

[英]Rails append new has_many through join record to nested form via javascript

I have a has_many through association and I am trying to set up a nested form that allows for the creation of new join/child record via javascript without leaving the parent form/page. 我有一个has_many通过关联,我试图建立一个嵌套表单,该表单允许通过javascript创建新的联接/子记录,而无需离开父表单/页面。 The below works for standard forms where a collection is rendered but I'm not sure what I'm missing for this nested form example. 以下内容适用于呈现集合的标准表单,但是我不确定此嵌套表单示例缺少什么。

Models: 楷模:

class FullApplication < ApplicationRecord
  has_many :fullapplication_districts, inverse_of: :full_application, dependent: :destroy
  has_many :districts, through: :fullapplication_districts

  accepts_nested_attributes_for :fullapplication_districts

class FullapplicationDistrict < ApplicationRecord
  belongs_to :full_application
  belongs_to :district

  validates_presence_of :full_application, :district
end

class District < ApplicationRecord
  has_many :fullapplication_districts, inverse_of: :district, dependent: :destroy
  has_many :full_applications, through: :fullapplication_districts
end

View: 视图:

<%= form_for(@full_application, url: full_applications_edit_path, method: :put) do |f| %>
  <div id="fullapplicationdistrictlist" class='row'>
    <%= f.fields_for :fullapplication_districts do |fad| %>
      <%= fad.collection_select :district_id, District.all, :id, :name, {include_blank: true}, {class: 'form-control'} %>
      <%= fad.number_field :percent_one, class: 'form-control', step: :any %>
      <%= fad.number_field :percent_two, class: 'form-control', step: :any %>
      <%= fad.number_field :percent_three, class: 'form-control', step: :any %>
      <%= link_to full_applications_districts_path(:id), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete this record?" } do %>
        <i class="fa fa-trash"></i>
      <% end %>
    <% end %>
  </div>
<% end %>
<%= link_to "Add New District", full_applications_districts_new_path(fullapplication_id: @full_application.id), method: :post, remote: true, class: "btn btn-form btn-primary", role: "button", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Creating..."} %>

fullapplication_districs_controller fullapplication_districs_controller

def create
    @full_application = FullApplication.find(params[:fullapplication_id])
    @fullapplication_district = FullapplicationDistrict.new(full_application_id: @full_application.id, district_id: District.first.id, percent_one: 0.0, percent_two: 0.0, percent_three: 0.0)
    respond_to do |format|
      @fullapplication_district.save
      format.js
    end
  end

create.js.erb create.js.erb

$('div#fullapplicationdistrictlist').append("<%= escape_javascript(render @fullapplication_district) %>");

Many thanks in advance for any insights. 非常感谢您的任何见解。

Check this railscast: http://railscasts.com/episodes/196-nested-model-form-part-1 检查此railscast: http: //railscasts.com/episodes/196-nested-model-form-part-1

Gives a lot of insight to the nested forms. 对嵌套表单有很多了解。 Part 2 also shows ways to add new nested fields dynamically. 第2部分还展示了动态添加新嵌套字段的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails嵌套属性form_for for has_many,使用javascript无限制嵌套模型 - Rails Nested Attributes form_for for has_many with unlimited nested models using javascript Rails form_for has_many通过使用AJAX的关联不起作用 - Rails form_for has_many through association using AJAX not working Rails:通过表单中的对象 ID 数组进行 has_many 关联 - Rails: has_many association through array of object IDs from form Nested_form_fields在验证失败时显示现有的has_many嵌套字段 - Nested_form_fields show existing has_many nested fields upon validation failure 如何在没有nested_form的情况下将表单写入has_many关联? - How to write form to has_many association without nested_form? Ember和Rails has_many关系 - Ember and Rails has_many relationship Rails 6 使用 has_many 关系和 accepts_nested_attributes_for 连接表 - Rails 6 Joins Tables using has_many relationships and accepts_nested_attributes_for Rails - 使用has_many保存模型:通过AngularJS层的关联 - Rails - Saving a model with has_many :through association from AngularJS layer 茧已在Rails中加载新的嵌套表单后,如何执行JavaScript? - How to execute JavaScript after cocoon has loaded a new nested form in Rails? Javascript-如何将用户输入附加到表单中的新列表项。 (如何添加记录) - Javascript - How to append user input to a new list item in a form. (how to add a record)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM