#1 楼 @pathbox
desc '创建账户信息' do
failure [
[498, '账户号已存在'],
[499, '操作失败'],
]
end
params do
requires :number, type: String, desc: '账户号', message: :requires
requires :unit, type: String, desc: '收费单位', message: :requires
requires :name, type: String, desc: '户主姓名', message: :requires
optional :address, type: String, desc: '户主地址'
optional :balance, type: String, desc: '账户余额'
optional :deleted, type: Boolean, desc: '账户未激活?'
end
post '/' do
if Fuel.exists?(number: params[:number])
error!('账户号已存在', 498)
end
if params[:balance] && params[:balance].to_f <= 0
error!('账户余额不合法', 400)
end
# @fuel = Fuel.new(params.permit(:number, :unit, :name, :address, :balance, :deleted))
@fuel = Fuel.new(declared(params, include_missing: false))
# 执行操作,失败直接返回
error_by!(@fuel.save)
# 正确返回
return_success
end
意图就是 create 的 api,然后主要问题在代码:
# @fuel = Fuel.new(params.permit(:number, :unit, :name, :address, :balance, :deleted))
@fuel = Fuel.new(declared(params, include_missing: false))
现在我是用第二句解决健壮参数问题(加上了gem 'hashie-forbidden_attributes'
以取消 Rails 的默认健壮参数),
我现在想问的是有没有办法以注释掉的那一句(原生 Rails 方法)的办法解决,因为我不想用gem 'hashie-forbidden_attributes'
这个 gem,我现在的 pramas 为:
{
"number": "8000113111",
"unit": "中国电力局",
"name": "李某"
}