How to use javascript variable in erb code in Rails?

I have a JavaScript variable:

var selectValUser = $('.select_user :selected').val();

I need to use this variable in erb code on the same page, i.e.

<%= get_free_hours_for_user(selectValUser) %>

How can i do this?

+1
source share
2 answers

You cannot do this, because javascript is executed on the client side, and the code in the ERB file works on the server side, you can send the value using an ajax request, Plus there are awsome cast rails here

javascript ; "Apply", "set_id" , , .., div "id_message" .

  $('#apply').live('click', function(event, data, status, xhr) {
    event.preventDefault();
    return $.ajax({
      url: '/users/registrations/set_id',
      type: 'GET',
      data: {
        code: $('#user_id').val()
      },
      success: function(data, event, status, xhr) {
        $('#id_message').html(response);
        return $("#id_message").show();
      },
      error: function(event, data, status, xhr) {
        $('#id_message').html(response);
        return $("#id_message").show();
      }
    });
  });

, .

+1

Source: https://habr.com/ru/post/1608570/


All Articles