【问题标题】:Rails has_many input from Angular2 front end won't save来自Angular2前端的Rails has_many输入不会保存
【发布时间】:2017-06-23 10:57:00
【问题描述】:

我正在构建一个从 Rails 5 API 获取数据的 Angular 2 前端应用程序。这是一种网络库存应用程序。

我在 Angular2 中有一个 Asset-form,并且该资产的 ip_addresses 有一个多选输入。

我无法让 Rails 在后端接受这些数据。

资产对象:

{"name":"SERVER_A","serial":"XOR-354","location_id":1,"asset_type_id":1,"model_id":3,"ip_address_ids":[5304,5305]}

这是我的asset.service.ts:

createAsset(asset: Asset){

let formData = new FormData();

for(var key in asset){
  if(key == "ip_address_ids") {
    for (var i = 0; i < asset[key].length; i++) {
        formData.append("asset["+key+"][]", JSON.stringify(asset[key][i]));
        console.log("asset["+key+"][]", JSON.stringify(asset[key][i]));
    }

  }
  if(key != "id") {
    formData.append("asset["+key+"]", asset[key]);
  }
}

let headers = new Headers();

let authToken = localStorage.getItem('auth_token');
headers.append('Authorization', authToken);  

return this.http.post(this.assetUrl, formData, { headers })
                .map((response: Response) => response.json());}

这是我在 Rails 服务器控制台中得到的:

Started POST "/assets" for ::1 at 2017-02-06 13:58:33 +0100 


Processing by AssetsController#create as HTML   


Parameters: {"asset"=>{"name"=>"SERVER_A", "description"=>"undefined",
"serial"=>"XOR-354", "asset_tag"=>"undefined",
"firmware"=>"undefined", "ssh"=>"undefined", "telnet"=>"undefined",
"http"=>"undefined", "location_id"=>"1", "asset_type_id"=>"1",
"model_id"=>"3", "prtg_id"=>"undefined", "ticket_id"=>"undefined",
"ip_address_ids"=>"5304,5305"}}   

Unpermitted parameter: ip_address_ids

我已经允许了 ip_address_ids 的参数

def asset_params
  params.require(:asset).permit(:name, :description, :serial, :asset_tag, :firmware, :ssh, :telnet, :http, :location_id, :asset_type_id, :model_id, :prtg_id, :ticket_id, :ip_address_ids => [])
end

奇怪的是,如果我在 Chrome 中使用 Advanced REST Client 是成功的。

Here's an image of the REST Client

Rails 服务器控制台中的结果:

Started POST "/assets" for ::1 at 2017-02-06 14:04:42 +0100

Processing by AssetsController#create as HTML

Parameters: {"asset"=>{"name"=>"Test", "asset_type_id"=>"2", "location_id"=>"33", "model_id"=>"4", "ip_address_ids"=>["5213", "5214"]}}

我认为问题在于 Angular 将 ID 作为字符串发送,而 REST 客户端将 ID 作为字符串数组发送。

知道如何解决这个问题吗?

【问题讨论】:

  • 尝试在您的模型(后端)上设置accepts_nested_attributes,并将ip_address_ids 的所有出现(前端和后端)更改为ip_address_ids_atrributes
  • 这不只是我想建立一个新的IP地址吗?该 IP 已存在于数据库中。
  • 你确定它已经存在了吗?正如您所提到的,它不允许您传递参数.. Unpermitted parameter: ip_address_ids.

标签: ruby-on-rails json api angular typescript


【解决方案1】:

您的ip_address_ids 参数是作为字符串而不是数组传递的,为什么不使用formData = JSON.stringify({asset: asset}) 而不是自定义处理?

如果您想将其作为实际的string 传递,则不应使用=&gt; [] 来允许它,只有:ip_address_ids 就足够了。

【讨论】:

  • 当我在执行 formData = JSON.stringify(asset) 时,我在 Rails 服务器控制台中收到此错误:Started POST "/assets" for ::1 at 2017-02-07 07:15:29 +0100 Processing by AssetsController#create as HTML Completed 400 Bad Request in 3ms (ActiveRecord: 0.7ms) ActionController::ParameterMissing (param is missing or the value is empty: asset):
  • 试试formData = JSON.stringify({asset: asset})
  • 我已经解决了!我现在只使用 JSON 并完全跳过了 FormData。我在asset.service.ts 中的POST 字符串现在看起来像这样:return this.http.post(this.assetUrl, JSON.stringify({asset: asset}), { headers }).map((response: Response) =&gt; response.json()); 我的asset-form.component.html 中也有这个:ngValue]="address.id.toString()" 我已将Rails 中asset_controller.rb 中的数组列入白名单: :ip_address_ids =&gt; []
猜你喜欢
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
相关资源
最近更新 更多