because the master detail fails ruby on rails

0

It is a detail teacher the father meetings the child is details_reuniones, details_reuniones, details_reuniones has association with product. In this form there is autocomplete and select.

is a nested form does not work is to add more products.

I click on the cart and it does not bring back the other cart and I reload the page.

MeetingsController

class ReunionesController < ApplicationController
before_action :set_reunion, only: [:show, :edit, :update, :destroy]

 autocomplete  :centro_costo, :fullname, :full => true, :column_name => 'fullname'

# GET /reuniones
# GET /reuniones.json
def index
  @reuniones = Reunion.all
  @negocios = Negocio.all
end
def select_region
  rs = Region.where(:negocio_id => params[:idnegocio]).order('nombre').all
  respond_to do |format|
  format.json {render json: rs }
    puts "funciono "
  format.html
  end
end

def select_ciudad
  rs = Ciudad.where(:region_id => params[:idregion]).order('nombre').all
  respond_to do |format|
  format.json {render json: rs }
  format.html
  end
end

def select_planta
  rs = Planta.where(:ciudad_id => params[:idciudad]).order('nombre').all
  respond_to do |format|
  format.json {render json: rs }
  format.html
  end
end


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

  # GET /reuniones/new
  def new
    Time.zone = 'America/Bogota' 
    @reunion = Reunion.new(fecha_entrega: Time.zone.now.strftime("%d/%m/%Y"))
    @reunion.detalles_reuniones.build
    @negocios = Negocio.all 
  end

  # GET /reuniones/1/edit
  def edit
    @negocios = Negocio.all
   end

# POST /reuniones
# POST /reuniones.json
def create

 centro_costo = CentroCosto.find_by(fullname: reunion_params[:centro_costo_fullname])
 @reunion = Reunion.new(reunion_params)
 @reunion.centro_costo_id = centro_costo.try(:id)
  respond_to do |format|
      if @reunion.save
      format.html { redirect_to @reunion, notice: 'La reunión se creó correctamente.' }
      format.json { render :show, status: :created, location: @reunion }
    else
    @negocios = Negocio.all
    format.html { render :new }
    format.json { render json: @reunion.errors, status: :unprocessable_entity }
  end
end
 end

  # PATCH/PUT /reuniones/1
  # PATCH/PUT /reuniones/1.json
  def update
    respond_to do |format|
      if @reunion.update(reunion_params)
         format.html { redirect_to @reunion, notice: 'La reunión se actualizó correctamente.' }
         format.json { render :show, status: :ok, location: @reunion }
      else
        @negocios = Negocio.all
        format.html { render :edit }
        format.json { render json: @reunion.errors, status: :unprocessable_entity }
      end
    end
  end

 # DELETE /reuniones/1
 # DELETE /reuniones/1.json
def destroy
   @reunion.destroy
    respond_to do |format|
    format.html { redirect_to reuniones_url, notice: 'Reunión fue destruida con éxito.' }
    format.json { head :no_content }
  end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def reunion_params
  params.require(:reunion).permit(:hora_pedido, :fecha_pedido,:hora_inicio, :hora_final, :fecha_entrega, :observacion, :subtotal,
  :planta_id, :ubicacion,:centro_costo_fullname,:hora_entre,:nombre,
  detalles_reuniones_attributes: [:id, :reunion_id, :cantidad, :valor, :producto_id, :_destroy])
end
end

meeting model

class Reunion < ApplicationRecord

 belongs_to :planta
 belongs_to :centro_costo

 #maestro de detalle
 has_many :detalles_reuniones, :dependent => :destroy
 accepts_nested_attributes_for :detalles_reuniones, allow_destroy: true

 def detalles_reuniones_for_form
     collection = detalles_reuniones.where(reunion_id: id)
     collection.any? ? collection : detalles_reuniones.build
 end



 #Autocomplete
  def centro_costo_fullname
    centro_costo.fullname if centro_costo
  end

  def centro_costo_fullname=(fullname)
   self.centro_costo = CentroCosto.find_by_fullname(fullname) unless fullname.blank?
  end

end

_form

  <%= simple_form_for(@reunion) do |f| %>
  <%= f.error_notification %>
  <div class="form-inputs">  
    <div class = "row">
    <div class = "col-md-4"></div>
    <div class ="col-xs-12  col-sm-6 col-md-4">

        <%= f.input   :nombre,label: "Nombre de reunion",
         placeholder: "Ingrese el nombre de  la reunion",input_html: { title: 'Nombre de la reunion' }%> 

    </div
    <div class = "col-md-4"></div>
    </div>

  <div class = "row">
    <div class = "col-md-2"></div>
    <div class = "col-xs-12  col-sm-6 col-md-2">


        <center><%= f.label :Hora_de_entrega %></center>
          <center><%= f.input :hora_entre,:label => false ,input_html: { title: 'Hora de entrega de los refrigerios' } %></center>
     </div>

    <div class ="col-xs-6  col-sm-6 col-md-2">
      <center><%= f.label :hora_inicio %></center>
      <center><%= f.input :hora_inicio,:label => false ,input_html: { title: 'Hora de inicio de la reunion' } %></center>

    </div>

    <div class = "col-xs-6  col-sm-6 col-md-2">

      <%= f.input :hora_final,input_html: { title: 'Hora en la que termina  la reunion ' } %>
    </div>

    <div class = "col-xs-12  col-sm-6 col-md-2">
      <%= f.input :fecha_entrega, as: :datetime_picker,
        input_html: {value:reunion.fecha_entrega.try(:strftime, '%d/%m/%Y'),
          data: {date_options: {format: 'DD/MM/YYYY', ignoreReadonly: true}}} 
          %>
    </div>
    <div class = "col-md-2"></div>
  </div>




     <div class = "row">
   <div class = "col-md-2"></div>
    <div class ="col-xs-12  col-sm-4 col-md-3">
      <label>Negocio</label>
      <%= select_tag "negocio", options_from_collection_for_select(@negocios, "id", "nombre"),
      class: "form-control", :include_blank => "Seleccione Pais"  %>
    </div>  

    <div class ="col-xs-12  col-sm-4 col-md-3">
      <label>region</label>
      <%= select_tag "region", "<option value="">Seleccione region</option>".html_safe, 
      class: "form-control" %>
    </div>

     <div class ="col-xs-12  col-sm-4 col-md-2">
     <label>ciudad</label>
     <%= select_tag "ciudad", "<option value="">Seleccione una ciudad</option>".html_safe,
      class: "form-control" %>
    </div>
    <div class = "col-md-2"></div>
</div>


  <div class = "row">
    <div class = "col-md-2"></div>
    <div class ="col-xs-12  col-sm-4 col-md-2">
        <label>Planta</label>
       <%= select_tag "reunion[planta_id]","<option value="">Seleccione una planta</option>".html_safe,
       class: "form-control" %>

    </div>
    <div class = "col-xs-12  col-sm-4 col-md-3">
      <%= f.input :ubicacion , placeholder: "Ingrese la ubicacion del salon",
      input_html: { title: 'La ubicacion donde se va llevar los refrigerios' }%>
    </div>
    <div class = "col-xs-12  col-sm-4 col-md-3">
     <%= f.input :centro_costo_fullname,label: "Centro de costo", :url => autocomplete_centro_costo_fullname_reuniones_path,
      :as => :autocomplete,input_html: { title: 'Centro de costos' } %>
    </div>
    <div class = "col-md-2"></div>
  </div>
</div>  
  </div> 
  <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 :detalles_reuniones do |detalles_reuniones_for_form| %>
       <%= render 'detalle_reunion_fields',  f: detalles_reuniones_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, :detalles_reuniones %>
     </div>
     <hr> 
   </div>
 <div class="col-md-2  col-sm-1 col-xs-1"></div>
 </div>



<div class = "row">
    <div class = "col-md-3"></div>
    <div class ="col-xs-12  col-sm-4 col-md-3">

      <%= f.input   :observacion ,label: "Campo de observacion",
      as: :text, :input_html => { :style=> '  resize: none; ',
      :size =>"5x5", :maxlength => 300 ,title: 'Observaciones '},
      placeholder: "Ingrese por que incumple con las politica del negocio. " %> 


    </div>
    <div class = "col-md-3">
      <%= f.input :subtotal,input_html: { title: 'Sudtotal de refrigerios' } %>
    </div>
    <div class = "col-md-3"></div>
    </div> 


 <div class="form-actions">
   <%= f.button :submit %>
 </div>
<% end %>

HELPER REUNION

 module ReunionesHelper

 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 |detalles_reuniones_for_form|
        render(association.to_s.singularize + "_fields", f: detalles_reuniones_for_form)
    end
     link_to(name, '', class: "add_fields", data: { id: id, fields: fields.gsub("\n", "")})
 end    

end

meetings.coffee

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

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

MODEL DETAIL_REUNION

class DetalleReunion < ApplicationRecord
 belongs_to :reunion, optional: true

  self.table_name = 'detalles_reuniones' 

  belongs_to :producto

 end

Log

When I click on the cart it returns and loads the page.

Hi look, I gave an example what I should do, the photos are in this link link thanks

    
asked by MIGUEL ANGEL GIL RODRIGUEZ 22.06.2017 в 17:37
source

1 answer

0

The 1 error is in the script within meetings.coffee , you must run event.preventDefault() before the rest of the code so that this have the desired effect.

Update your meetings.coffee file like this:

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))

1 Having event.preventDefault() at the end, this does not prevent the link that is clicked (eg with the class .remove_fields ) from loading the URL that has a reference (in the attribute href ); in this case, because the link generated with link_to_add_fields points to the same page where it is (i.e. new.html.erb ), the same page is reloaded when clicking on it.

    
answered by 28.06.2017 в 17:58