Actioncontroller::Routingerror (No route Matches {:action=>"show", :controller=>"license_report"}):

Asked

Viewed 36 times

0

I am creating a menu to access part of relay of some system features, but when creating the controller of license_report, when I try to access the path "license_report_path"

He presents me with this mistake Actioncontroller::Routingerror (No route Matches {:action=>"show", :controller=>"license_report"}):

my Routes.Rb this way :

resources :license_report, only: [:index, :show]

My license_report_controller.Rb looks like this:

# encoding: utf-8
class LicenseReportController < ApplicationController
    before_filter :require_user
    before_filter :set_contractual_instrument, only: [:show]
  
  
    def index
        includes = [:cr_program, :highway]
        @contractual_instruments = CrContractualInstrument.includes(includes).order(:ic_number)
        @contractual_instruments = @contractual_instruments.select { |ci| ci.valid_inspections.any? }
    end
  
    def show
        @license = License.where("cr_contractual_instrument_id = #{params[:id]}")
        byebug

        respond_to do |format|
            byebug
            format.html do
              add_breadcrumb 'Relátorios de licenças', 'license_report_path'
            end
            format.pdf do
                byebug
              pdf = LicenseReportPDF.new(@license.map{|license| license.id})
              send_data pdf.render, filename: pdf_filename, type: 'application/pdf', disposition: :inline
            end
        end
    end
  
    private

    def set_contractual_instrument
      @contractual_instrument = CrContractualInstrument.find(params[:id])
    end
  
    def pdf_filename
      time = Time.current.strftime('%Y-%m-%d-%H%M%S')
      name = @contractual_instrument.ic_number.gsub('/', '-')
  
      "#{name}_#{time}.pdf"
    end

end

If I withdraw any mention of license_report_path the code returns to work normally.

How do I add this route, it should be included in Resources or I’m thinking wrong?

2 answers

1

this is because the license_report_path has the mandatory parameter id if you do something like

license_report_path(params[:id])

or

license_report_path(id: params[:id])

must solve.

  • Thanks!! It worked, but. I wanted him to enter without mandatory parameter and I could not visualize where "I set" this parameter as mandatory.

  • 1

    the Resources :license_report, only: [:show] that generates this dependency of an ID, to make a show without this dependency, you can use the Resource that will look like this: Resource :license_report, only: [:index, :show] , or more rubocop Resource :license_report, only: %i[index show]

0


The ideal is to leave the controller pluralized, ex: Licensereports.

So when you put Resources on the routes, it will create the routes for the actions, being the index:

license_reports_path

new:

new_license_report_path

Edit:

edit_license_report_path

and the others (show, update and Destroy):

license_report_path

but passing the search parameter (id), ex:

license_report_path(id: params[:id])

Not being plural can come to problems as it has happened to me

  • 1

    Remembering that in the "Edit" action you also need to pass the parameter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.