I need to place a checkbox in my rails form that when it is checked and the user saves the form, it will be redirected to another page, but only when said checkbox is checked and this is a field that will not be in the base of data. How could I do it? I had thought of something like this:
<label class="control-label col-md-3">Utiliza Envase:</label>
<div class="col-md-1">
<input type="checkbox" id='showAreaBox' class="producto_Utl.Env">
</div>
and in the create.js.erb:
$('#showAreaBox').change(function() {
// if the checkbox is checked, invoke the link
if(this.checked ) {
$(window.location.replace("/Productos/<%= @producto.id %>/envase"))
}
// checkbox is unchecked
// else {}
});
the controller:
class ProductosController < ApplicationController
before_action :set_producto, only: [:show, :edit, :update, :destroy]
before_action :authenticate_usuario!
# GET /productos
# GET /productos.json
def index
@productos = Producto.activos.por_empresa(current_usuario.empresa_id)
@producto = Producto.new
end
# GET /productos/1
# GET /productos/1.json
def show
end
# GET /productos/new
def new
@producto = Producto.new
end
# GET /productos/1/edit
def edit
end
# POST /productos
# POST /productos.json
def create
@producto = Producto.new(producto_params)
respond_to do |format|
if @producto.save
format.html { redirect_to @producto, notice: 'Producto was successfully created.' }
format.json { render :show, status: :created, location: @producto }
format.js {flash.now[:notice] = 'El producto se ha creado de forma exitosa.'} #ajax
else
format.html { render :new }
format.json { render json: @producto.errors, status: :unprocessable_entity }
format.js {flash.now[:alert] = 'Error al crear el producto.'} #ajax
end
end
end
# PATCH/PUT /productos/1
# PATCH/PUT /productos/1.json
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 {flash.now[:notice] = 'El producto se ha actualizado de forma exitosa.'} #ajax
else
format.html { render :edit }
format.json { render json: @producto.errors, status: :unprocessable_entity }
format.js {flash.now[:alert] = 'Error al actualizar el producto.'} #ajax
end
end
end
# DELETE /productos/1
# DELETE /productos/1.json
def destroy
respond_to do |format|
format.html { redirect_to productos_url, notice: 'Producto was successfully destroyed.' }
format.json { head :no_content }
format.js {flash.now[:notice] = 'El producto se ha borrado de forma exitosa.'} #ajax
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_producto
@producto = Producto.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def producto_params
params.require(:producto).permit(:Clave, :Producto, :CodBarras)
end
end