redirect using a checkbox

2

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
    
asked by LuisC 26.10.2016 в 05:59
source

1 answer

1

You do not really need to save anything in the database. A form will send a request to the server (your Rails application) with the information you want. This request will be managed by your controller. What you decide to do with this information will depend on the controller code. In other words, the model and the controller are independent.

In this case, your form must have a 'name' attribute so that its value is included in the form's request. It could be something like this:

label class="control-label col-md-3">Utiliza Envase:</label>
          <div class="col-md-1">
            <input type="checkbox" id='showAreaBox' name='envase' class="producto_Utl.Env">
          </div>

Notice that I have added the field 'name' with 'container' Now, when you send the data of your form, the controller will receive it as a parameter that you can control from params.

Therefore, in your controller you could do something like this:

def create
  @producto = Producto.new(producto_params)

  respond_to do |format|
    if @producto.save
      if params[:envase]
        #Responde aquí con lo que quieras, por ejemplo:
        format.html { redirect_to envase_path, notice: 'Ha seleccionado envase.' }
      else
        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
      end
    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
    
answered by 26.10.2016 / 22:31
source