There are Strong Parametrs:

params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data) 

The data field is of JSON type, the database is PG

When saving / updating, the field remains empty.

I tried to write:

  props = params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data) props[:data] = params[:author][:data] 

So:

  props = params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data) props[:data] = params[:author][:data].to_hash 

Tries to make an association and naturally nothing happens. I tried converting to json, but this is a string after all:

  props = params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data) props[:data] = params[:author][:data].to_json 

Postgres on data type swears.

To sort through the controller is a lot and somehow wrong. But there is a need to store data with dynamic fields and the ability to sample them.

Thank you for your help!

  • This option is similar to: params.require (: author) .permit (: firstname,: secondname,: patronymic,: position, data: {group: {}}) PG: ActiveRecord :: StatementInvalid: PG :: UndefinedTable: ERROR: missing FROM-clause entry for table "data" LINE 1: ... secondname "= $ 3 AND" authors "." Position "= $ 4 AND" data "." Gr ... - user3401370
  • It must somehow be solved easily - user3401370
  • params [: video] .require (: author) .permit (: firstname,: secondname,: patronymic,: position, data: []) Sets the default value to user3401370
  • params [: video] .require (: author) .permit (: firstname,: secondname,: patronymic,: position, data: {}) Writes an empty json - user3401370
  • Generally speaking, the model and the database have a little to do with it. Okay, you need to skip something in the key :author > :data . By the way, what do you send there? - D-side

1 answer 1

Such cases are listed in the guides as "outside the Strong Parameters area" .

If you know in advance valid keys with scalars (single values, which are not hashmaps and arrays) in a nested hashmap, you can specify them explicitly:

 params.require(:author) .permit(:firstname, :secondname, :patronymic, :position, data: [:one, :two, :three]) 

... and if not, and you just want to allow "anything," then Strong Parameters is no longer your assistant. It is designed for typical cases when there is a white list of keys. So you have to spin up the results of his work: do a selection of the keys that you know, and then throw in the data in it, the keys in which you are not interested. For example:

 author = params.require(:author) author.permit(:firstname, :secondname, :patronymic, :position) .merge(author.slice(:data)) # <= "выщипывает" ключ-значение только для :data # ^^^^^ как обычный Hash#merge 

Based on comments: remember that for the type of json operator = does not exist. Therefore, searching for the whole data field will not work either in find_or_create or in validates ..., :uniqueness , anywhere. It exists for jsonb , but that's another story.