带有Rails和Ember的JSONAPI强大参数 [英] JSONAPI strong params with Rails and Ember

查看:52
本文介绍了带有Rails和Ember的JSONAPI强大参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Ember与ember-data和rails api一起使用。我有一个createRecord()和save()用于运行良好的记录。在网络选项卡中,用于发布请求以在Rails中创建记录的有效负载如下: {data:{属性:{foo:‘bar’}}} \\ code>。

I'm using Ember with ember-data and a rails api. I had a createRecord() and save() for the record that was working fine. The payload in the network tab for the post request to create the record in rails looks like: {data: {attributes: { foo: 'bar' } }.

在rails控制器中,我有很强的参数,例如: params.require(:data).require(:attributes).permit(: foo),它工作了一段时间。现在,当我发送请求时,rails说 param丢失或值为空:data 。如果我在浏览器的网络选项卡中查找,则请求的有效负载仍然与上述内容相同。如果我放入参数,则仅显示 { controller: api / v1 / answers, action: create} 并且根本不显示数据有效载荷。

In the rails controller, I have strong params like so: params.require(:data).require(:attributes).permit(:foo), which was working fine for a little while. Now when I send the request rails says that param is missing or the value is empty: data. If I look in the network tab in the browser the payload for the request still looks the same as stated above. If I puts params it only shows {"controller": "api/v1/answers", "action": "create"} and isn't showing the data payload at all.

是否有任何原因导致Rails现在无法从余烬中获取正确的参数?我确实尝试向要创建的模型添加关联,这是模型开始失败的时候。但是,我回滚到它何时可以工作,但现在不再工作。

Is there any reason why rails isn't picking up on the right params from ember now? I did try to add an association to the model that I'm trying to create, which is when it started failing. However, I rolled back to when it was working, but it's not working anymore.

推荐答案

我通过进入< Rails api中的code> config / initializers / mime_types.rb 文件并将文件更改为:

I fixed this by going into the config/initializers/mime_types.rb file in the rails api and changing the file to look like:

api_mime_type = %W(
  application/vnd.api+json
  text/x-json
  application/json
)

Mime::Type.unregister :json
Mime::Type.register 'application/json', :json, api_mime_type

这篇关于带有Rails和Ember的JSONAPI强大参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆