M
M
Max2016-04-03 16:42:58
Ruby on Rails
Max, 2016-04-03 16:42:58

Why does the view not see what is in the variable?

Hello, tell me how to deal with the variable.
I'll start from the beginning. There are for example 100 div blocks on the site. Each div is a place in the cinema. When clicking on one of the blocks, its unique id is written to the $arr variable. Thus, I implement the functionality for ordering and selecting several places for booking at once.

$arr = [];
      $link = $(location).attr('href').slice(36);
            $('.btn-warning').each(function() {
                $place_id = $(this).attr('id');
                $arr.push($place_id);
            });

            $.ajax({
                url: "/film_sessions/" + $link,
                type: "get",
                data: { data_value: JSON.stringify($arr) }
            });

Then I get this array using ajax request and send it to the rails controller.
before_action :get_booking_place_array, only: [:show]

def get_booking_place_array
      @test_test = params[:data_value]
      puts "----------------------------------------------------------------------"
      puts @test_test
      puts "----------------------------------------------------------------------"
    end

def show
...
    @test_test = []
  end

The output of the received data is planned in the modal window
<div id="data2">
      <% @test_test.each do %>
        <h1>hello</h1>
      <% end %>
    </div>

The problem is that if the test_test variable is not declared in the show action and an empty array is not assigned to it, then when entering the show page, an error will occur - undefined method `each' for nil:NilClass.
Otherwise, it will always be equal to an empty array. That is, if I select several places (divs) and go to the terminal, I see such entries there.
["place_5","place_6","place_32"]
I understand that the entry is displayed thanks to the puts test_test line in the get_booking_place_array method.
However, with the 'web-console' gem, I can check that the test_test variable on the page itself is equal to an empty array ( [] ).
It turns out that there is data in the terminal in the variable, but they are not recognized on the view, and it turns out that I use 2 different variables. How to fix it? )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Andreev, 2016-04-04
@maxprof

Why do you need before_action for one action?
Try to do this in show:
Or in the view, check your

<% if @test_test.present? %>
.....
<% end %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question