Rails 4.PSQL 9.3。托管在 Heroku 上。我用 angularJS 发送数组。
在我的 heroku 服务器上,我检查了我的日志,确实参数是作为一个数组发送的。我还可以确认它正在路由到正确的 update
路由。这是 heroku 日志:
INFO -- : Parameters: {"profile"=>{"order"=>[0, 1, 2, 3, 4]}, "id"=>"123"}
尽管正确发送了参数,但 Rails 不会将此数组保存到我的 activerecord 中。为什么?
整数数组是通过数据库迁移
add_column :profiles, :order, :integer, array: true
我的更新路线是这样的:
def update
@profile = Profile.find(params[:id])
if @profile.update(profile_params)
render json: @profile
else
render 'failed to update'
end
end
我将 profile_params 定义为:
def profile_params
params.require(:profile).permit(...some other variables, :order)
end
所有非常标准的东西所以我不知道我做错了什么
在使用 angularJS 的前端,我有以下内容:
var params = {
profile:{
order: [0,1,2,4]
}
}
$http.put("http://my_server_url/123", params)
.success(function(data,status,headers,config){
console.log(data)
})
知道我在这里做错了什么吗?
编辑:弄清楚出了什么问题,仍然需要帮助
所以在做了一些测试后,我发现当我检查 profile_params 时它是空的。为什么发送数组时为空?
最佳答案
看起来问题已在此链接中得到解答:http://jaketrent.com/post/permit-array-rails-strong-parameters/
基本上,您需要以不同方式允许数组。它需要像这样被允许:
params.require(:model).permit(:other_field_1,:other_field_2,... my_array_field:[])
关于ruby-on-rails - 无法在 Rails 事件模型中保存整数数组...为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645045/