gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:49:36 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com