0

Coming from php, not sure why the symbol below is used on the right hand side of to.

get '/posts/new', to: 'posts#new'

Is this a hash which to is the key and whats in the quotes are the key?

Been looking at Rails tutorials and never seen a hash in this kind of form so I was wondering...

dragon
  • 101
  • 4

1 Answers1

2

Yes, you are correct, the to: is a hash key.

When a hash is the last argument in the list Ruby allows you to forego the use of the curly braces.

Perhaps it is clearer when we add the parentheses and curly braces:

get('/posts/new', { to: 'posts#new' })

That line calls the get method with two arguments. The first argument is the string '/posts/new'. The second one is the hash { to: 'posts#new' }.

spickermann
  • 100,941
  • 9
  • 101
  • 131