api我的控制器檔案夾中有一個子檔案夾。所以當我打電話時,POST "/api/auth"我希望程式使用 rails 約定到達那里。IE我不想為每個呼叫撰寫路由,而是使用 rails“專利”,使 rails 轉到 CRUD 操作,理解 PUT、POST、GET 本身。
所以在我的routes.rb我有:
namespace :api do
resources :debts, :auth
end
但是當我發布(或獲取)時,localhost:3000/api/auth我得到:
ActionController::RoutingError (uninitialized constant Api::AuthController)
我錯過了什么?
請注意,我還需要在子檔案夾中有許多控制器。有match適合所有人的嗎?
uj5u.com熱心網友回復:
你也必須把你的控制器放在一個子檔案夾中然后做例如
# /app/controllers/api/debts_controller.rb
module Api
class DebtsController < ApiController
end
end
# /app/controllers/api/auth_controller.rb
module Api
class AuthController < ApiController
end
end
然后在基本控制器檔案夾中:
# /app/controllers/api_controller.rb
class ApiController < ApplicationController
end
uj5u.com熱心網友回復:
您還需要命名該類才能使其正常作業。
可以使用以下2種方式:
module Api
class AuthController < ApplicationController
# Your controller code here
end
end
class Api::AuthController < ApplicationController
# Your controller code here
end
如果您有一些代碼需要為 Api 命名空間內的每個控制器運行,您可以創建一個 Api 基本控制器,但這不是必需的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398137.html
標籤:红宝石轨道
上一篇:Ruby中的物件方法是什么
