Rails 4: strong parameters and array of scalars must be listed last on permit? -


see people_controller#person_params method code version of question:

# person.rb class person < activerecord::base   # attributes:   # - names (string)   # - age (integer)    # combine: ["a", "b", "c", ...] => "a,b,c"   def names=(values)     self[:names] = values.join(",") if values.present?   end end  # people_controller.rb class peoplecontroller < applicationcontroller   def create     @record = record.new(person_params)     @record.save!   end    def person_params     params.require(:person).permit(       # works fine       :age,       names: []        # works fine       { names: [] },       :age        # not work (syntaxerror)       names: [],       :age     )   end end 

the question is, why names scalar array not work when list @ beginning without wrapping hash?

the http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters doc examples don't wrap scalar arrays hash, aren't complex examples, either.

is expected behavior strong_parameters?

this not strong_params thing, rather how ruby reading list of attributes. in ruby, can omit curly brackets around hash when the last argument method, call:

any_method(arg1, arg2, key: value, foo: :bar) 

is read as:

any_method(arg1, arg2, { key: value, foo: :bar }) 

you cannot omit brackets if hash not last argument though, hence this:

any_method(arg1, key: value, arg2) 

will raise syntax error.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -