Hortitude
Hortitude

Reputation: 14058

rails routes except without symbol

I ran across some routes that followed this format:

resources :foobar, except: "create"

I was just wondering how it worked to have "except:" instead of the symbol ":except" as I see in most documentation.

What is actually happening in this case? Is it calling a method called "except"? What does the colon do?

Upvotes: 0

Views: 147

Answers (1)

mu is too short
mu is too short

Reputation: 434835

Ruby 1.9 supports a JavaScript-ish Hash syntax so this:

resources :foobar, except: 'create'

is the same as this:

resources :foobar, :except => 'create'

The new syntax has limitations though:

  1. The Hash key must be a symbol.
  2. The key can't be something that you'd normally quote so you can't use it for symbols like :'this.that'.
  3. You can't use it with symbols such as :$set (which appear all over the place if you're using MongoDB).

I'm not sure what the exact restrictions are as I don't use it (I do a fair bit of MongoDB work and I have a thing for consistency) but I think the symbols need to match /^[a-z_]\w*/i (or technically, anything that can be used as a label) to be used with the new format.

Upvotes: 2

Related Questions