[英]ActionController::Parameters converts integer to string
我在 rails 5 中定義user_params
,如下所示:
def training_plan_params
params.require(:training_plan).permit(
:training_plan_id,
:sport_id,
:distance,
lastTrainingStatus: [:duration,:reps,:type,:level],
trainings: [:text, :order, :level],
modified: [:level,:duration]
)
end
但是當我將參數發送到 Rails 時,如下所示:
{ training_plan: { sport_id: 2, distance: 900 } }
它不會將控制器中的距離檢索為整數,如下所示:
=> <ActionController::Parameters {"sport_id"=>"2", "distance"=>"900"} permitted: true>
=> training_plan_params[:distance]
=> "900"
有沒有辦法保持其類型而不是將其轉換為字符串?
今天也在處理同樣的問題,我做了一些搜索。 我發現這篇文章: Rails 測試正在將我的整數數組轉換為字符串數組
基本上,問題在於您(我們)使用 ActionDispatch::IntegrationTest 的方式,而不是 ActionController::Parameters。 我通過使用.to_json
將輸入轉換為我的 PUT/POST 調用並將headers: { 'Content-Type': 'application/json' }
提供給 PUT/POST 調用來解決這個問題。 然后導致測試路由器將我的數據視為 json 並保留類型,而不是默認將事物視為文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.