[英]Getting rails-api and strong_parameters to work together
包括時
gem 'strong_parameters'
gem 'rails-api'
在我的Gemfile
,調用params.require
就好
private
def user_params
params.require(:user).permit(:first_name, :last_name)
end
在require()
調用上失敗並出現以下錯誤。
TypeError:
can't convert Symbol into String
回溯顯示strong_parameters
的ActionController::StrongParameters
的require()
方法永遠不會被命中。
我在這個上花了太長時間,所以我想我會在這里分享,希望能節省一些時間。
上面的錯誤來自於調用時執行的ActiveSupport::Dependencies::Loadable
中的require()
方法
params.require(:user)...
strong_parameters
將ActionController::StrongParameters
注入到此文件底部的ActionController::Base
中
ActionController::Base.send :include, ActionController::StrongParameters
rails-api
gem需要你的app的ApplicationController
擴展ActionController::API
,轉而使用ActionController::Base
應用程序控制器對ActionController::StrongParameters
因為它們沒有擴展ActionController::StrongParameters
類。 這就是require()
方法調用不調用ActionController::StrongParameters
的實現的ActionController::StrongParameters
。
告訴ActionController::API
關於ActionController::StrongParameters
就像將以下內容添加到config/initializers
的文件一樣簡單。
ActionController::API.send :include, ActionController::StrongParameters
我有一個應該解決此問題的拉取請求 (當前打開)。 而不是調用ActionController::API.send
,這應該包括在......
ActiveSupport.on_load(:action_controller) do
include ActionController::StrongParameters
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.