Javascript 使用迭代更新嵌套表单属性

Javascript 使用迭代更新嵌套表单属性,javascript,ruby-on-rails,ruby,nested-forms,cocoon-gem,Javascript,Ruby On Rails,Ruby,Nested Forms,Cocoon Gem,我目前正在尝试使用嵌套形式与茧,是可怕的!!但我正面临审问,我不知道该走哪条路 关系: 牧场有奶牛 农场有生产 产品具有嵌套形式的IPProduction 我现在要做的是将我的迭代| c |实现到我的每个I产品中:cow|u id元素 我的代码: (我的表格制作)= (我的IPProductions\u字段)= {:cow_id=>:cow}%> (我的控制器)= class ProductionController

我目前正在尝试使用嵌套形式与茧,是可怕的!!但我正面临审问,我不知道该走哪条路

关系: 牧场有奶牛
农场有生产 产品具有嵌套形式的IPProduction

我现在要做的是将我的迭代| c |实现到我的每个I产品中:cow|u id元素

我的代码:

(我的表格制作)=


(我的IPProductions\u字段)=


{:cow_id=>:cow}%>
(我的控制器)=

class ProductionController
我的真实视图,因此我希望使用生成IPProductions_字段的按钮分配每个IPProduction

如果你知道我可以用什么方法来实现这个目标,欢迎你, 提前谢谢

这是不是和你的问题一样?区别是什么?这和你的问题是一样的吗?有什么区别?
 <%- @cows.each do |c| %>
<div class="row">
  <div class="col-md-12">
    <div id="Order">
      <%= f.simple_fields_for :iproductions do |ip| %>
      <%= render 'iproductions_fields', f: ip, cow: c %>
      <%end%>
      <div class="Order_links">
        <%= link_to_add_association c.name, f, :iproductions, cow: c, class: "btn btn-default" %>
      </div>
    </div>
  </div>
</div>
    <div class="form-inline clearfix">
  <div class="nested-fields">
    <%= f.input :cow_id, :input_html => { :cow_id => :cow }  %>
    <%= f.input :quantity, input_html: {class: "form-input form-control"} %>
    <%= link_to_remove_association "Supprimer", f, class: "form-button btn btn-default" %>
  </div>
</div>
class ProductionsController < ApplicationController
  before_action :authenticate_user! 
  skip_before_action :configure_sign_up_params
  before_action :set_ranch
  before_action :set_production, except: [:create, :new, :show]
  before_action :set_production2, only: [:show]

  # GET /productions
  # GET /productions.json
  def index
    @productions = Production.all
  end

  # GET /productions/1
  # GET /productions/1.json
  def show
    @iproductions = Iproduction.where(id: params[:production_id])
  end

  # GET /productions/new
  def new
    @production = @ranch.productions.build
    @cows = @ranch.cows
  end

  # GET /productions/1/edit
  def edit
    @cows = @ranch.cows
  end

  # POST /productions
  # POST /productions.json
  def create
    @production = @ranch.productions.create(production_params)
    @production.update(date: Date.today)
    @cows = @ranch.cows
    respond_to do |format|
      if @production.save
        format.html { redirect_to ranch_production_path(@production.ranch_id, @production), notice: 'Production was successfully created.' }
        format.json { render :show, status: :created, location: @production }
      else
        format.html { render :new }
        format.json { render json: @production.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /productions/1
  # PATCH/PUT /productions/1.json
  def update
    respond_to do |format|
      if @production.update(production_params)
        format.html { redirect_to ranch_production_path(@production.ranch_id, @production), notice: 'Production was successfully updated.' }
        format.json { render :show, status: :ok, location: @production }
      else
        format.html { render :edit }
        format.json { render json: @production.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /productions/1
  # DELETE /productions/1.json
  def destroy
    @production.destroy
    respond_to do |format|
      format.html { redirect_to ranch_productions_path(@production.ranch_id), notice: 'Production was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.

    def set_ranch 
      @ranch = Ranch.find(params[:ranch_id])
    end 

    def set_production2 
      @production = Production.find_by(id: params[:id])
    end 

    def set_production 
      @production = @ranch.productions.find_by(id: params[:id])
    end 

    # Never trust parameters from the scary internet, only allow the white list through.
    def production_params
      params.require(:production).permit(:name, :ranch_id, :created_at, :date, iproductions_attributes: [:id, :date, :cow_id, :quantity, :_destroy])
    end
end