【发布时间】:2023-03-14 13:36:01
【问题描述】:
这快把我逼疯了。 Rails 说我没有定义控制器动作,但我显然在 rake routes 中定义了。
我在这里错过了什么?
spec/controllers/api/v1/files_controller_spec.rb:
describe "download action" do
after do
get :download, id: file.id
end
it "returns http status 200 OK" do
expect(response).to have_http_status(200)
end
end
没有路由匹配动作的失败消息download:
Failures:
1) Api::V1::FilesController download action returns http status 200 OK
Failure/Error: get :download, id: file.id
ActionController::UrlGenerationError:
No route matches {:action=>"download", :controller=>"api/v1/files", :id=>"2"}
config/routes.rb:
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :files, only: [:index, :create]
get "files/:id", to: "file#download", as: "file"
end
end
rake 路线 | grep 文件:
api_v1_files GET /api/v1/files(.:format) api/v1/files#index {:format=>:json}
POST /api/v1/files(.:format) api/v1/files#create {:format=>:json}
api_v1_file GET /api/v1/files/:id(.:format) api/v1/file#download {:format=>:json}
【问题讨论】:
-
您是否在您的
API::V1::FilesController中定义了公共方法download? -
还没有,正在尝试 TDD 这个东西。
-
不确定,但如果您改用
to: "files#download"(复数),也许您的方式也可以。
标签: ruby-on-rails ruby routes