I'm trying to make a form by adding a variable related to the table, it would be a product that has a product and a product that belongs to a product, I followed this tutorial: link and I am also implementing the ajax application through this other tutorial: link my problem is that the edit form is not taking the attached variable that is "pzaxcaja" but if you are taking it in the create form, I guess it is because the controller is specified: @producto.build_productosxpza
and in the edit form is specified in this way <%= form_for(producto, :method => :put, remote: true, html: {class: "form-horizontal"}) do |f| %>
This is the method index my controller (where I have the two create and edit forms):
def index
@productos = Producto.all
@producto = Producto.new
@producto.build_productosxpza
end
my index.html.erb
<!--<p id="notice"><%= notice %></p>-->
<h1>Lista de productos</h1>
<style>
.container {
width: 100%;
}
</style>
<div class="container">
<div class="row">
<div class="text-center">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mynewproducto">
Nuevo producto
</button>
</div>
</div>
<br>
<br>
<table id="productos" class="display"><!--el id productos es de datatables referenciado en productos.coffe y display class es una clase de datatables-->
<thead>
<tr><!--active es para sombrear la fila-->
<th>Clave</th>
<th>Producto</th>
<th>Sector</th>
<th>Codbarras</th>
<th>Vbase</th>
<th>Iva</th>
<th>Ban envase</th>
<th>Unimed</th>
<th>Foto</th>
<th>Acciones</th>
<th></th>
</tr>
</thead>
<tbody id="container_productos">
<%= render @productos %><!--carga todos los productos-->
</tbody>
</table>
<!-- Modal create action -->
<%= form_for(@producto, remote: true, html: {class: "form-horizontal"}) do |f| %> <!--ajax remote: true-->
<div class="modal fade" id="mynewproducto" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Agregar producto</h4>
</div>
<div class="modal-body">
<div class="form-group">
<%= f.label :clave, "Clave:", class: "control-label col-md-3" %>
<div class="col-md-7">
<%= f.text_field :clave, class: "form-control producto_clave" %>
</div>
</div>
<div class="form-group">
<%= f.label :producto, "Producto:", class: "control-label col-md-3" %>
<div class="col-md-9">
<%= f.text_field :producto, class: "form-control producto_producto" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.fields_for :productosxpza do |productosxpza| %>
<div class="productosxpza_fields">
<div class="field">
<%= productosxpza.label :pzaxcja, "Piezas X Caja:", class:"control-label col-md-3" %>
<div class="col-md-3">
<%= productosxpza.number_field :pzaxcja, class: "form-control producto_pzaxcja" %>
</div>
</div>
</div>
<%end%>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="mynewproductoclose">Close</button>
<%= submit_tag "Create", class: "btn btn-primary"%>
</div>
</div>
</div>
</div>
<%end%>
</div>
my partial _product.html.erb
<tr id="producto_<%= producto.id %>">
<td><%= producto.clave %></td>
<td><%= producto.producto %></td>
<td><%= producto.sector %></td>
<td><%= producto.codbarras %></td>
<td><%= producto.vbase %></td>
<td><%= producto.iva %></td>
<td><%= producto.ban_envase %></td>
<td><%= producto.unimed %></td>
<% if producto.cover? %>
<td id="producto-imagen" ><%= image_tag producto.cover.url(:minithumb) %></td>
<%else %>
<td></td>
<%end%>
<td>
<button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myupdateproducto_<%= producto.id %>">
Edit
</button>
<!--Destroy-->
<%= link_to 'Destroy', producto, method: :delete, class: "btn btn-danger btn-xs", remote:true %>
</td>
<td class="no" >
<!--Modal - update producto-->
<%= form_for(producto, :method => :put, remote: true, html: {class: "form-horizontal"}) do |f| %><!--ajax-->
<div nohidden class="modal fade si" id="myupdateproducto_<%= producto.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Editar producto</h4>
</div>
<div class="modal-body">
<div class="form-group">
<%= f.label :clave, "Clave:", class: "control-label col-md-3" %>
<div class="col-md-7">
<%= f.text_field :clave, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :producto, "Producto:", class: "control-label col-md-3" %>
<div class="col-md-9">
<%= f.text_field :producto, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.fields_for :productosxpza do |productosxpza| %>
<div class="productosxpza_fields">
<div class="field">
<%= productosxpza.label :pzaxcja, "Piezas X Caja:", class:"control-label col-md-3" %>
<div class="col-md-3">
<%= productosxpza.number_field :pzaxcja, class: "form-control producto_pzaxcja" %>
</div>
</div>
</div>
<%end%>
</div>
<div class="modal-footer">
<button type="button" id="myupdatebutton_<%= producto.id %>" class="btn btn-default" data-dismiss="modal">Close</button>
<%= submit_tag "Update", class: "btn btn-primary"%>
</div>
</div>
</div>
</div>
<%end%></td>
</tr>
my product model:
class Producto < ActiveRecord::Base
self.primary_key = "clave"
has_one :productosxpza, class_name: "Productosxpza", foreign_key: "producto_id"
accepts_nested_attributes_for :productosxpza
end
my productsxpza model:
class Productosxpza < ActiveRecord::Base
self.primary_key = "idp"
belongs_to :producto, class_name:"Productosxpza"
end
the code update in the products driver:
def update
respond_to do |format|
if @producto.update(producto_params)
format.html { redirect_to @producto, notice: 'Producto was successfully updated.' }
format.json { render :show, status: :ok, location: @producto }
format.js #ajax
else
format.html { render :edit }
format.json { render json: @producto.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
def producto_params
params.require(:producto).permit(:clave, :producto, :codbarras, :granel, :iva, :ieps, :unimed, :vbase, :equivalente, :sector, :ban_envase, :idclasp, :idempresa, :status, :producto_id, :cover, productosxpza_attributes: [:id, :idp, :pzaxcja, :producto_id ])
end
the update.js.erb
$("#myupdateproducto_<%= @producto.id %>").modal('hide');
$("#producto_<%= @producto.id %>").fadeOut(500, function(){
$(this).remove();
$("#container_productos").prepend('<%= j render @producto %>');
});