Home avoid losing the value of variable sessions
Reply: 0

avoid losing the value of variable sessions

luis31
1#
luis31 Published in 2017-11-08 20:34:08Z

in my view index I am showing a table with multiple records, each record has a checkbox where it has the id like this

  <td>

      <%= check_box_tag "items[]", pedido.IdPedido, false,  :id => "#{pedido.IdPedido}"%>

      <script>
      $('#<%=pedido.IdPedido%>').click(function() {
        var items;
        var consolidar;
        if ($(this).is(':checked')) {
          items = ["<%=pedido.IdPedido%>"];
          consolidar = true;
        } else {
          items = "<%=pedido.IdPedido%>";//para borrarlo debe ser sin el arreglo
          consolidar = false;
        }
        $.ajax({
            type: "GET",
            url: "/consolidar",
            data: { id: $(this).data('post-id'), items: items, consolidar: consolidar }
         });
      });
      </script>
  </td>

when clicking on the checkbox it is sent automatically by ajax the id to the action called "consolidacion" in that action I need to fill an array with all the ids received

this is mi consolidacion action:

    def consolidacion

        if (@items == nil)
          @items = params[:items]
        elsif (params[:items] != nil)
          params[:items].each do |course|
            @items.push("#{course}") #fill the array
          end
        end

        respond_to do |format|
          format.js {flash.now[:notice] = 'ok'} #ajax
        end
      end

the problem is that after sending the first id and executing it the second time sending another id the array @items is nil.

to avoid losing the values within the array, I shared the value of the variable @items through "sessions" by placing two, the first to receive it from my index view and the second to share it with my index view

  def consolidacion
    @items = session[:items] 

    if (@items == nil)
      @items = params[:items]
    elsif (params[:items] != nil)
      params[:items].each do |course|
        @items.push("#{course}") #fill the array
      end
    end

    session[:items] = @items

    respond_to do |format|
      format.js {flash.now[:notice] = 'ok'} #ajax
    end
  end




  def index
      @items = session[:items]
      session[:items] = @items

    respond_to do |format|
      format.html
      format.js 
    end
  end

but now the problem is that when I check multiple checkboxes at the same time and the ids are sent some are not entering it in the array and I think it is because when it is executed multiple times and it has not been shared with the session in register previous in the last line of code, when sending to the second tap at the beginning of the action the variable @items of index that was not yet updated, so it usually ends up adding only the last record sent, unlike check them one by one waiting to finish executing the action to select the other. How could I solve this? or there would be some better way to avoid losing the value of the variable items at the time of executing the "consolidacion" action

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.326237 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO