ruby - Ruby 强参数:Integer 的 NoMethodError 未定义方法允许
问题描述
我的控制器:
class V1::SendContractController < V1::BaseController
def create
byebug
bride_membership = Wedding.find(send_params[:weddingId]).bride_memberships[0]
SendBrideContractJob.perform_now(bride_membership, send_params[:contractId])
render json: { enqueuedDelivery: true }, status: :ok
end
private
def send_params
params
.require(:weddingId)
.permit(:contractId)
end
end
我的参数
Parameters: {"weddingId"=>4, "contractId"=>20, "send_contract"=>{"weddingId"=>4, "contractId"=>20}}
错误
NoMethodError (undefined method `permit' for 4:Integer):
但是当我byebug
得到它时,我得到了我想要的!
(byebug) params
<ActionController::Parameters {"weddingId"=>4, "contractId"=>20, "controller"=>"v1/send_contract", "action"=>"create", "send_contract"=>{"weddingId"=>4, "contractId"=>20}} permitted: false>
(byebug) params[:weddingId]
4
我正在使用axios
拦截器来处理格式问题:
axios.interceptors.request.use((config) => {
if(config.url !== "/authentications") {
config.paramsSerializer = params => {
// Qs is already included in the Axios package
return qs.stringify(params, {
arrayFormat: "brackets",
encode: false
});
};
axios.defaults.headers.common['Authorization'] = `Bearer ${store.state.token.token}`
config.headers.common['Authorization']= `Bearer ${store.state.token.token}`
axios.defaults.headers.common['Accept'] = 'application/vnd.bella.v1+json'
config.headers.common['Accept'] = 'application/vnd.bella.v1+json'
return config
}
return config
})
解决方案
我相信 require 会为您提供您提供的密钥对象,以进行进一步的 permit 和/或 require 调用。
也许您可以尝试(未测试):
params.require(:weddingId)
params.permit(:weddingId, :contractId)
推荐阅读
- sql-server - 我希望将表 2 中的一些数据和一些常量插入表 1
- python - UnboundLocalError:赋值前引用的局部变量——添加为全局变量?
- c++ - 如何停止/中断当前循环(序列)以运行新数据(序列)?
- c - 在 C 中执行时如何处理 Windows 路径空间
- excel - 从图表 Excel 中仅删除 0 个值
- spark-koalas - Spark-Koalas 错误:列分配不支持类型元组
- reactjs - 为什么点击图标后我的自定义模态框打不开?
- ireport - iReport 3.7.3 在 Windows 10 上启动时崩溃
- javascript - 如何设置 JSON 对象的大小限制?
- angular - 使用量角器,如果该“元素”是具有 id 属性的角度组件选择器,您能否通过 id 找到元素?