small medium large xlarge

Generic-user-small
23 Jun 2012, 00:15
John Goetz (6 posts)

I am working through the AWDwR4edp2-2. Chapter 8, page 92 has us add a line to routes.rb to redirect the root to the store’s index page. The comments use a different syntax, and I’m trying to understand why the book does it differently:

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
root to: 'store#index', as: 'store'

So two questions: why use method #2? How can you add the ‘as: store’ part to method #1?

Dave_gnome_head_isolated_pragsmall
23 Jun 2012, 16:51
Dave Thomas (390 posts)

The book uses the new Ruby 1.9 syntax for hash literals, The folks that bring you Rails have not yet updated all their code, and this comment is an example of the older syntax (that still works.

You’d add the as parameter using

~~~ root :to => 'store#index', :as => 'store' ~~~

Basically, in a hash literal, x: y is the same as :x => y

Generic-user-small
24 Jun 2012, 01:18
John Goetz (6 posts)

Thanks, Dave, that explains a lot! I sorta guessed that x: y was the same as :x => y, but didn’t know the right search terms to find confirmation out there in g-land.

You must be logged in to comment