0
votes

Im using Cocoon gem(https://github.com/nathanvda/cocoon) in my Rails 4 application connected to a Postgres database for building a nested model form. I have permitted the attributes of the nested model (owner) in my projects controller as follows

params.
        require(:project).
        permit(:name, :description,
               owners_attributes: [:name, :email, :miscellanous, :_destroy])

miscellanous column in owners table is of type json . Im unable to specify miscellanous correctly in the required params above. Suppose miscellanous can have the following keys: favorite_color and favorite_movie. How can i specify that in the strong params above ?

If i do the following:

params.
    require(:project).
    permit(:name, :description,
           owners_attributes: [:id, :_destroy, :name, :email, miscellanous_attributes: [:favorite_color, :favorite_movie] ])

I get syntax error, unexpected ']', expecting =>

How can i correctly permit the json column called miscellanous for the nested model owners using strong params ?

1
On which line do you get the error, because the strong parameters declaration seems perfectly ok at first sight. How do you define the json attributes, maybe something is wrong there?nathanvda

1 Answers

0
votes

A few things:

  1. You can permit a hash with unknown keys with an empty hash bracket.
  2. Don't forget you need the ID for your owners.
  3. Note the missing 'e' in miscellaneous.

Try something like this:

params.
    require(:project).
    permit(:name, :description,
           owners_attributes: [:id, :name, :email, miscellaneous: {}, :_destroy])