ruby-on-rails - ActionController::UrlGenerationError:没有路由匹配 - 我的错误在哪里?

标签 ruby-on-rails

这是我对章节 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

这是我的routes.rb

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

这是我的 chapters.yml 夹具:

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

这是 chapters_controller.rb 的(部分):

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

我不知道为什么会收到此错误消息?

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>'

但是当我将“/chapter_nklm/12/show”复制到浏览器中时,一切正常吗?我的错误在哪里,我没有理解哪个概念?

提前致谢!

最佳答案

get 正在等待接收作为符号对象的操作,您正在传递整个路由别名以及所需的对象。

尝试在你的路由中使用 params 选项:

test 'should get show_nklm' do
  get :show_nklm, params: { id: chapters(:one).id }
  assert_response :success
end

关于ruby-on-rails - ActionController::UrlGenerationError:没有路由匹配 - 我的错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49967235/

相关文章:

ruby-on-rails - Rails 嵌套 with_option :if used in validation

ruby-on-rails - 如何附加回WEBrick服务器

ruby-on-rails - Rails - 做简单的除法?没那么简单

ruby-on-rails - 查找去年的月份名称

ruby-on-rails - 使数据库列可选或不需要,以便我可以创建记录

ruby-on-rails - rspec 无法识别 Postgres View

ruby-on-rails - Pengwynn 领英 Gem : How to follow a company

ruby-on-rails - `rbenv install 2.2.0` 错误

ruby-on-rails - 带有 Rails 3.0 的 Google map API

ruby-on-rails - Acton 文本,显示具有事件存储的视频播放器