【问题标题】:Cant save integer array in rails active model...why?无法在 Rails 活动模型中保存整数数组...为什么?
【发布时间】:2014-12-26 00:35:37
【问题描述】:

Rails 4. PSQL 9.3。托管在 Heroku 上。我用 angularJS 发送我的数组。

在我的 heroku 服务器上,我检查了我的日志,并且确实将参数作为数组发送。我也可以确认它正在路由到正确的update 路由。这是heroku日志:

INFO -- :   Parameters: {"profile"=>{"order"=>[0, 1, 2, 3, 4]}, "id"=>"123"}

尽管正确发送参数,Rails 不会将这个数组保存在我的活动记录中。为什么?

整数数组是通过db migration添加的

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 时,它是空的。为什么发送数组时是空的?

【问题讨论】:

    标签: ruby-on-rails arrays angularjs postgresql activerecord


    【解决方案1】:

    看起来这个问题在这个链接中得到了回答:http://jaketrent.com/post/permit-array-rails-strong-parameters/

    基本上,您需要以不同的方式允许数组。它需要像这样被允许:

    params.require(:model).permit(:other_field_1,:other_field_2,... my_array_field:[])

    【讨论】:

      猜你喜欢
      • 2012-02-09
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多