Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionController::UrlGenerationError: No route matches - where is my mistake?

This is my test for the chapters controller

require 'test_helper'

class ChaptersControllerTest < ActionController::TestCase

    test "should get index_nklm" do
        get :index_nklm
        assert_response :success
    end

    test "should get show_nklm" do
        get show_chapter_nklm_path(chapters(:one))
        assert_response :success
    end
end

This is my routes.rb

 get 'chapters_nklm' => 'chapters#index_nklm', as: :chapters_nklm
 get 'chapter_nklm/:id/show' => 'chapters#show_nklm', as: :show_chapter_nklm

This is my chapters.yml fixture:

one:
  id: 12
  chapter_name: MyString
  chapter: MyString
  description: MyText
  display: 1
  subject_id: 1

This is (part of) the chapters_controller.rb:

def show_nklm
  @chapter = Chapter.find(params[:id])
end

And I don't know why I get this error message?

Error:
ChaptersControllerTest#test_should_get_show_nklm:
ActionController::UrlGenerationError: No route matches {:action=>"/chapter_nklm/12/show", :controller=>"chapters"}
    test/controllers/chapters_controller_test.rb:11:in `block in <class:ChaptersControllerTest>'

But when I copy "/chapter_nklm/12/show" into the browser, everything works fine? Where is my mistake and which concept didn't I understand?

Thanks in advance!

like image 349
theorix Avatar asked Dec 06 '25 04:12

theorix


1 Answers

get is waiting to receive an action as a symbol object, you're passing the whole route alias, plus the object needed.

Try using the params option with your route:

test 'should get show_nklm' do
  get :show_nklm, params: { id: chapters(:one).id }
  assert_response :success
end
like image 75
Sebastian Palma Avatar answered Dec 07 '25 19:12

Sebastian Palma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!