My master detail does not work ruby on rails

0

Hi look, I gave an example what I should do, the photos are in this link link but it does not work I recharge the page every time I give it to add another one or if I am going to delete it, I always recharge the page thanks for the attention given.

An example of the database.

snack.rb

class Snack < ApplicationRecord
   self.table_name = 'snacks'
   belongs_to :planta
   belongs_to :centro_costo
   belongs_to :productonack

   has_many :detallenacks, :dependent => :destroy
   accepts_nested_attributes_for :detallenacks, allow_destroy: true

   def detallenacks_for_form
     collection = detallenacks.where(snack_id: id)
     collection.any? ? collection : detallenacks.build
   end

end

productonack.rb

class Productonack < ApplicationRecord
  self.table_name = 'productonacks'
  belongs_to :planta
  belongs_to :negocionac
  belongs_to :detallenack, optional: true
end

detallenack.rb

class Detallenack < ApplicationRecord
  self.table_name = 'detallenacks' 
  belongs_to :snack
  belongs_to :productonack
end

snacks_controller.rb

class SnacksController < ApplicationController
  before_action :set_snack, only: [:show, :edit, :update, :destroy]

  # GET /snacks
  # GET /snacks.json
  def index
    @snacks = Snack.all
  end

  # GET /snacks/1
  # GET /snacks/1.json
  def show
  end

  # GET /snacks/new
  def new
    @snack = Snack.new
    @snack.detallenacks.build
  end

  # GET /snacks/1/edit
  def edit
  end

  # POST /snacks
  # POST /snacks.json
  def create
    @snack = Snack.new(snack_params)

    respond_to do |format|
     if @snack.save
       format.html { redirect_to @snack, notice: 'Snack was successfully created.' }
        format.json { render :show, status: :created, location: @snack }
     else
        format.html { render :new }
        format.json { render json: @snack.errors, status: :unprocessable_entity }
    end
   end
  end

 # PATCH/PUT /snacks/1
 # PATCH/PUT /snacks/1.json
 def update
    respond_to do |format|
     if @snack.update(snack_params)
       format.html { redirect_to @snack, notice: 'Snack was successfully updated.' }
       format.json { render :show, status: :ok, location: @snack }
      else
        format.html { render :edit }
        format.json { render json: @snack.errors, status: :unprocessable_entity }
      end
    end
 end

 # DELETE /snacks/1
 # DELETE /snacks/1.json
 def destroy
   @snack.destroy
     respond_to do |format|
       format.html { redirect_to snacks_url, notice: 'Snack was successfully destroyed.' }
       format.json { head :no_content }
     end
 end

 private
   # Use callbacks to share common setup or constraints between actions.
  def set_snack
    @snack = Snack.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def snack_params
    params.require(:snack).permit(:hora_pedido, :fecha_pedido,
      :hora_inicio, :hora_final, :fecha_entrega, :observacion,
      :planta_id, :centro_costo_id, :ubicacion, :nombre,
      :hora_entrega, :snackfullnombre, :snackfullname, :productonack_id,
        detallenacks_attributes: [:id,:productonack_id, :cantidad, :snack_id, :_destroy])
  end
end

snacks.coffee

jQuery ->
$(document).on 'click', '.remove_fields', (event) ->
  event.preventDefault()
  $(this).prev('input[type=hidden]').val('1')
  $(this).closest('fieldset').hide()

$(document).on 'click', '.add_fields', (event) ->
  event.preventDefault()
  time = new Date().getTime()
  regexp = new RegExp($(this).data('id'), 'g')
  $(this).before($(this).data('fields').replace(regexp, time))

snacks_helper.rb

module SnacksHelper
   def link_to_add_fields(name, f, association)
      new_object = f.object.send(association).klass.new
      id = new_object.object_id
      fields = f.fields_for(association, new_object, child_index: id) do |detallenacks_for_form|
         render(association.to_s.singularize + "_fields", f: detallenacks_for_form)
       end
     link_to(name, '', class: "add_fields", data: { id: id, fields: fields.gsub("\n", "")})
   end


 end  

_form.html.erb

 <%= simple_form_for(@snack) do |f| %>
 <%= f.error_notification %>
  otros campos
  este partial redirecciona 
  <div class="row">
   <div class=" col-sm-1 col-xs-1 col-md-2"></div>
   <div class="col-xs-10  col-sm-10  col-md-8 ">
     <hr>
     <div class="row compact wello">
       <%= f.fields_for :detallenacks do |detallenacks_for_form| %>
       <%= render 'det_fields',  f: detallenacks_for_form %>
       <% end %>
       <%= link_to_add_fields '<i class="fa fa-cart-arrow-down fa-3x color-red"  
     aria-hidden="true"></i>'.html_safe,  f, :detallenacks %>
     </div>
     <hr> 
     </div>
     <div class="col-md-2  col-sm-1 col-xs-1"></div>
   </div>
<div class="form-actions">
  <%= f.button :submit %>
</div>
<% end %>

_det_fields.html.erb

 <fieldset>
   <div class="row compact">
     <div class="col-md-1"></div>
     <div class="col-md-4">
        <strong>producto</strong>
     </div>
     <div class="col-md-2">
        <strong>Cantidad  </strong>
     </div>
     <div class="col-md-5">

     </div>
  </div>

  <div class="row compact">
    <div class="col-md-1"></div>
    <div class="col-md-4">
      <%= f.association :productonack, label: false, class: "form-control", 
      prompt:"Seleccione el insumo" %>
    </div>
   <div class="col-md-2">
     <%= f.input :cantidad, label: false, class: "form-control" ,
     placeholder:"La cantidad " %>
   </div>
   <div class="col-md-5">

  </div>


   <%= f.hidden_field :_destroy %>
   <%= link_to "Eliminar", '#', class: "remove_fields btn btn-danger" %>
</div>

LOG

WHEN I GIVE YOU CLICK

    
asked by juan gomez 25.08.2017 в 15:42
source

1 answer

0

First you should check the relationships you have done well, according to your ER, the type of relationship you have is many to many.

Since the detail table has its own properties, it is better to use through to access them.

we should also review the migration.

snack.rb

class Snack < ApplicationRecord
   has_many :detallenacks, :dependent => :destroy, through: :detallesnack
end

Detail snack.rb

class Detallenack < ApplicationRecord
  belongs_to :snack
  belongs_to :productonack
end

product snack.rb

class Productonack < ApplicationRecord
  has_many :snack, through: :detallesnack
end
    
answered by 25.08.2017 в 17:59