I am working on an application where I send a post request to my rails server with parameters stored in JSON format. Let's say my application routes the request to the create function on my cats_controller. The cat model has three fields :name, :hunger, :mood. Now each cat has_many kittens. My Kitten model has two fields :cat_id (referring to which cat owns it, because my kitten belongs_to a cat) and :cute. Now every time i create a cat on my application I want to do it with a single call, so I call /cats.json from my app with a POST request. The parameters are stored in JSON format and they include the following fields {name:"",hunger:"", mood:"",cute:""}. Now the controller takes these params creates a new cat, and then creates a new kitten assigned to this cat using cat.kittens.build(). The kitten then just needs to use the last parameter I sent "mood:" to get created properly.
Now the question is this, when I print the params variable from this controller I get the following hash: {name:"", hunger:"", mood:"", cute:"", cat:{name:"", hunger:"", mood:""}}. Why does this happen? How does Rails parse the POST request params and take me from
{name:"",hunger:"", mood:"",cute:""} to {name:"", hunger:"", mood:"", cute:"", cat:{name:"", hunger:"", mood:""}}
How is this "cat" hash generated, when, and what rules does it follow?
Then my followup question would be, since rails 4 forces you to whitelist parameters before you use them. I am doing:
params.require(:cat).permit(:name,:hunger,:mood)
How do I also permit the :cute value?