我在 Rails 上構建了一個 API,并且在這個框架中是新的。我想向 API 添加狀態路由。例如,在本地運行,如果我向端點發出請求:GET /status (http://localhost:3000/status) 它應該必須回傳狀態代碼 204,僅此而已。
我該怎么做?
我正在考慮創建一個名為 status 的模型并只使用 index 方法,呈現 status::204,但我認為必須有一個更簡單的方法。
謝謝!
uj5u.com熱心網友回復:
Status
在此示例中不需要模型。
將新路線添加到您的config/routes.rb
:
resource :status, only: [:show] # note the singular `resource` instead `resources`
app/controllers/statuses_controller.rb
并在(此處為復數)添加一個帶有此內容的新控制器;
class StatusesController < ApplicationController
def show
head :no_content
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/507441.html