K
K
kukushka1542021-09-27 15:57:11
JavaScript
kukushka154, 2021-09-27 15:57:11

How to run javascript code in ruby ​​on rails?

There is an input.html.erb file, through it I fill out a form for entering a natural number n and enter n numbers, when the button is pressed, a JS script should be launched that processes this request using the Get method, but it does not start

. This worked on 4 versions of Ruby on Rails, when the javascript codes were on the route: "app/assets/javascripts", now these codes are on the route "app/javascript/packs", I think this is the problem.

The program itself sees this file for sure, since when the server starts it is visible (screen below) that the file is being processed, I tried to add the code to input.html.erv: <%= javascript_pack_tag 'sequence', 'data-turbolinks-track': 'reload ' %>

Controller code:

# app/controllers/sequence_controller.rb
 
class SequenceController < ApplicationController
  before_action :authorize
 
  def input
    begin
      a = params[:value].to_s
      b = params[:numbers].to_s
      c = a + " " + b
    rescue
      p 'SequenceController: Input data error'
      p c
    end
    # a = 0 if a < 0
    respond_to do |format|
      format.html
      format.json do
        render json: { solution: Answer.where(query: c).first_or_create.get_solution}
      end
    end
    # p Answer.last
    # p ActiveSupport::JSON.decode(Answer.last.solution)
    # p JSON.parse(Answer.all.to_json).to_xml
  end
end

input.html.erb code:
<!-- app/views/sequence/input.html.erb -->
<form id="form" action="javascript:">
<h1>Последовательность</h1>
  <label>Введите натуральное число:</label>
  <input type="number" pattern="^[\d]+$" name="value" /> <br> <br>
  <label>Введите числа через пробел:</label>
  <input type="text" name="numbers"/> <br> <br>
  <input type="submit" class = "btn btn-primary" value="Найти последовательность"/>
</form>
<table id="table" style="display:none" class="table table-hover">
  <tr>
    <th>Н</th>
    <th>Исходная последовательность</th>
    <th>Подходящие последовательности</th>
    <th>Длиннейшая последовательность</th>
    <th>Кол-во последовательностей</th>
  </tr>
</table>
<div id="error" style="display:none" class = "alert alert-danger">
  <strong>Ошибка!</strong> Timeout error
</div>
<script>
  <%= javascript_pack_tag 'sequence', 'data-turbolinks-track': 'reload' %>
</script>

sequence.js code:
(function(){
  'use strict';
  $(window).one('load', function() {
    let $Form = $('#form');
    let $Table = $('#table');
    let $Error = $('#error');
 
    $Form.on('submit', function() {
      let $HTML = '';
      $Table.find('tr:not(:first)').remove();
      $.get('/sequence/input.json', $Form.serialize(), function($JSON) {
        if($JSON['solution'].length === 0) {
          $Table.hide();
          $Error.show();
        }
        else {
          $.each($JSON['solution'], function($NULL, $Value) {
            $HTML += '<tr>\
                        <td>' + $Value[0] + '</td>\
                        <td>' + $Value[1] + '</td>\
                        <td>' + $Value[2] + '</td>\
                        <td>' + $Value[3] + '</td>\
                        <td>' + $Value[4] + '</td>\
                      </tr>';
          });
          $Table.show().append($HTML);
          $Error.hide();
        }
      });
    });
  });
})();


The page itself and the dock in the absence of any actions when you click on the button
6151bf51539a9861025951.png

What should have happened
6151bf6ac0727823371635.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question