![](/img/trans.png)
[英]How to create a route under a nested resource and namespace in rails?
[英]Rails resource route under namespace leading to “not a supported controller name.”
我正在Rails中構建一個API,並且希望對其進行版本控制。 我的routes.rb
文件包括:
Rails.application.routes.draw do
namespace :V1 do
resources :users
end
end
我在/app/controllers/V1/users_controller.rb
下有我的控制器,它具有以下內容:
module V1
class UsersController < ApplicationController
def index
render json: {message: "This is a test!"}
end
end
end
當我嘗試在命令行上運行Rails服務器時,出現以下錯誤:
`default_controller_and_action': 'V1/users' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
我查看了錯誤消息給我的鏈接,但是,這似乎是如何指定要與資源一起使用的控制器。 而不是這樣做,Rails不能從我的目錄結構中自動確定它嗎?
試試這個:
Rails.application.routes.draw do
namespace :v1 do
resources :users
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.