JSONAPI strong params with Rails and Ember

拜拜、爱过 提交于 2019-12-23 12:36:20

问题


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' } }.

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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/35733430/jsonapi-strong-params-with-rails-and-ember

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!