Wednesday, 14 April 2021

Rails 6.1.3 link_to route is not working with no error code, simply ignoring it

I am using Rails 6.1.3 with Ruby 2.7.2 for a mostly static pages app. The app has a registration form that a student must complete and download as a PDF file to print, persistence is not required at this point. It was working fine with Dhalang (Google's puppeteer wrap) gem, then I did a Javascript routine for a different part of the app and it stopped to work. The process should be: A view has a button link_to the route "new_student_url" set up to the "students_controller#new" action which should get the views/students/new to render the _form, At this point I have the button pointing and recognizing the route but when I click on it it just ignores the event; Oddly enough, if I right click the button to 'open link in a new tab', it works... =/ I have read several other cases and found that most of them are caused by a Turbolinks issue, so I did review my Turbolinks setup with Webpack and the app/javascript/packs/application.js seems to be ok, please help.

This is the link button:

<%= link_to "Cédula de Registro", new_student_url, class: "btn btn-success btn-lg", style: "color:#fff;", :data => { :turbolink => 'false' } %>

The data: Turbolinks set to false is an attempt to solve the issue found in another post. If I hover the pointer over the button I can see the route is well pointed to

These are my related routes:

  get 'students/nuevo_ingreso', to: 'students#nuevo_ingreso', as: 'nuevo_ingreso'
  resources :students
  controller :students do
    get '/convert/:id', to: 'students#convert', as: 'convert'
    get '/pdf/:id', to: 'students#pdf', as: 'pdf'
  end

I can see the "$rails routes" are well defined and the link point to the right route This is the students_controller.rb section

class StudentsController < ApplicationController
  before_action :set_student, only: %i[ show edit update destroy pdf ]

  # GET /students or /students.json
  def index
    @students = Student.all
  end

  # GET /pdf
  def pdf
    # this method will be called by Dhalang
  end

  # GET /convert
  def convert
    _url = request.base_url + pdf_path
    _pdf = Dhalang::PDF.get_from_url(_url)
    _file_name = "CEBcedulaInscripcion"
    File.open("#{Rails.root}/public/#{_file_name}.pdf", "w+b") << _pdf
      redirect_to "/#{_file_name}.pdf"
  end

  # GET /students/1 or /students/1.json
  def show
  end

  def nuevo_ingreso
    
  end
  
  # GET /students/new
  def new
    @student = Student.new
  end

  # GET /students/1/edit
  def edit
  end

  # POST /students or /students.json
  def create
    @student = Student.new(student_params)
    
    respond_to do |format|
      if @student.save
        format.html { redirect_to convert_path(@student) }
        format.json { render :show, status: :created, location: @student }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end

I am including the view parts just for completeness This is the students/new.html.erb

<div class="container col-md-6">
  <h1>CÉDULA DE INSCRIPCIÓN</h1>
  <%= render 'form', student: @student %>
</div>
<%= link_to 'Back', students_path %>

And this is the students/_form.html.erb

<%= bootstrap_form_for(@student) do |f| %>

<div class="control-box">
  <h4>Datos Personales</h4>
  <div class="field">
    <%= f.text_field :name, label: "Nombre", class: "form-control-sm" %>
  </div>
  <div class="form-row">
    <div class="col-md-4">
      <div class="field">
        <%= f.text_field :email, label: "Email", class: "form-control-sm" %>
      </div>
    </div>
...
<div class="control-box">
  <h4>Seguridad Social</h4>
  <div class="row">
    <div class="col-md-4">
      <div class="field">
        <%= f.select :security_institution, [["ISSSTE", "ISSSTE"], ["IMSS", "IMSS"], ["SSA", "SSA"], ["Otro", "Otro"]], { label: "¿Cuenta con algún servicio Medico?", wrapper: { class: 'form-control-sm', data: { foo: 'bar' } } }, { class: "selectpicker" } %>
      </div>
    </div>
    <div class="col-md-8">
      <div class="field">
        <%= f.text_area :medical_condition, label: "¿Recibe actualmente algún tratamiento Médico?", class: "form-control-sm" %>
      </div>
    </div>
  </div>
</div>



<div class="container center">
  <div class="row">
    <div class="actions">
      <%= f.submit "Generar PDF", target: "_blank" %>
    </div>
  </div>
</div> 

<% end %>

And lastly, the Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3'
gem 'pg', '~> 1.1'
gem 'font-awesome-rails'
gem 'mini_racer'
gem 'puma', '~> 5.0'
gem 'sass-rails', '>= 6'
# Use bootstrap forms
gem 'bootstrap_form', '~> 4.0'
gem 'webpacker', '~> 5.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'

gem 'Dhalang'

gem 'bootsnap', '>= 1.4.4', require: false

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'pry-rails'
end

group :development do
  gem 'web-console', '>= 4.1.0'
  gem 'rack-mini-profiler', '~> 2.0'
  gem 'listen', '~> 3.3'
  gem 'spring'
end

group :test do
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver'
  gem 'webdrivers'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

This is the javaScript code which is imported ( import "packs/static_pages" ) in the app/javascript/packs/application.js

$(function () {
  $('.menu li:has(ul)').on("click", function(e) {
    e.preventDefault();
    if ($(this).hasClass('activado')) {
      $(this).removeClass('activado');
      $(this).children('ul').slideUp();
    } else {
      $('.menu li ul').slideUp();
      $('.menu li').removeClass('activado');
      $(this).addClass('activado');
      $(this).children('ul').slideDown();
    }
  });
});


from Rails 6.1.3 link_to route is not working with no error code, simply ignoring it

No comments:

Post a Comment