0

So, there is a working part of code in rails 5. I'm making huge form with nested attributes on many levels. I've got an strong params list:

params.require(:lexeme).permit(
    :word, :homonym_number, :see_other_lexeme_id, :semantic_description, :afterword,
    variants_attributes: [ :word, :id, :_destroy, proofs_attributes: [ :word, :language_id, :meaning, :id, :_destroy, language_mode_ids: [] ] ]
  )

And when I tried to change order a little bit...

params.require(:lexeme).permit(
    :word, :homonym_number, :see_other_lexeme_id, :semantic_description, :afterword,
    variants_attributes: [ :word, :id, :_destroy, proofs_attributes: [ :word, :language_id, :meaning, :id, language_mode_ids: [], :_destroy ] ]
  )

I'm getting:

syntax error, unexpected ']', expecting =>
...guage_mode_ids: [], :_destroy ] ]
...                              ^):

So, rails masters, what is wrong with this simple syntax here, has always array parameters need to be placed as last? What if I have more arrays in params?

1 Answer 1

1

The problem is that the last :_destroy should be a key => value according to the syntax sugar lexical parsing.

If you want to retain the new order you need to make language_mode_ids: [] a literal Hash like so:

params.require(:lexeme).permit(
  :word, :homonym_number, :see_other_lexeme_id, :semantic_description, :afterword,
  variants_attributes: [ :word, :id, :_destroy, 
    proofs_attributes: [ 
      :word, :language_id, :meaning, :id, {language_mode_ids: []}, :_destroy 
    ] 
  ]
)

Ruby is trying to help you by allowing you to specify language_mode_ids: [] but it will require all the arguments on the right hand side of that to also represent a key value pair.

Since :_destroy is just a symbol the interpreter does not know what to do with this. That is why your first order worked and the new order does not.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for clarifying. Sometimes syntax helpers could be confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.