mardi 6 décembre 2016

Emberjs PATCH and POST to Rails unpermitted parameters

So I've got an EmberJS and Rails API going.

I am using Active Model Serializer.

I have the following Active Model Serializer intializer config:

active_model_serializer.rb

ActiveModelSerializers.config.adapter = :json_api

api_mime_types = %W(
  application/vnd.api+json
  text/x-json
  application/json
)
Mime::Type.register 'application/vnd.api+json', :json, api_mime_types

At the moment, my Ember app is able to fetch data from my Rails API fine.

It is having problem sending updated user info to my Rails server however.

I am getting an error on my Rails server log:

Unpermitted parameters: data, id, user

My ember appears to be sending the paramters:

{
    "data"=>{
        "id"=>"2",
        "attributes"=>{
            "first-name"=>"James",
            "last-name"=>"Raynor",
            "username"=>"Jimobo",
            "email"=>"jim@gmail.com",
            "photo"=>{
                "photo"=>{
                    "url"=>nil
                }
            }
        },
        "type"=>"users"
    },
    "id"=>"2",
    "user"=>{

    }
}

Note: I only changed the username from Jimo to Jimobo.

When I was building my Rails API, I was using Active Model Serializer and the strong parameters looks something like:

Strong Parameters

def user_params
  params.permit(:first_name, :last_name, :username, :email, :password, :password_confirmation, :photo, friend_ids: [])
end

Do I really have to go through every one of my strong parameter declarations and modify them to be like this:

params.require(:data).require(:attributes).permit(...);

Or is there some magical solution I am not doing correctly?

In this Github discussion http://ift.tt/2gLQsJ3, a user named rmcsharry mentioned he didn't really need to modify anything and it worked out of the box......although he didn't show what his strong parameters looked like.




Aucun commentaire:

Enregistrer un commentaire