1

The problem

/app/controllers/users_controller.rb:29: syntax error, unexpected ',', expecting => name, roles: [], (something here)

The scenario

In users_controller.rb:

params.require(:user).permit(:name, roles: [])

Diagnostics

  • The problem happens when I access any page related to users_controller.rb;
  • If I use :roles => [] instead of roles: [], the error is the same;
  • If I change roles: [] for :roles, everything goes okay.

Environment

I'm running on CentOS 6.5 with Rails 4.

Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96
  • possible duplicate of http://stackoverflow.com/questions/18436741/rails-4-strong-parameters-nested-objects – hawk Aug 04 '14 at 17:23
  • 1
    I don't believe this is a dupe of that. This is a syntax error in ruby, that was a question about how to use strong params. – ABMagil Aug 04 '14 at 17:55

1 Answers1

3

The comma is unexpected because it interprets :name as a hash key. This is happening because Rails is seeing the hash roles: []. Explicitly mark it as such

params.require(:user).permit(:name, {roles: []})
ABMagil
  • 4,579
  • 4
  • 21
  • 35
  • As I said, I'm using Rails 4... Is [this](http://stackoverflow.com/questions/8826407/rails-3-multiple-select-with-has-many-through-associations) question wrong? – Guilherme Oderdenge Aug 04 '14 at 17:33
  • Ruby has two valid ways to interpret the parameters you're passing to `permit`, as a hash or as an array which contains a hash. It seems to have chosen the former for some reason. I'll admit myself surprised, but take a look at [this answer](http://stackoverflow.com/a/20633975/2483138) to dive into how Ruby interprets parameter lists. – ABMagil Aug 04 '14 at 17:53
  • In the answer linked, the parser correctly understands that the arguments are an array followed by a hash. I suspect you would need to dive into the source code for the strong_params gem for more clarity – ABMagil Aug 04 '14 at 17:57