Hash confusion illustrated

15 Mar

Just a bit of code to illustrate the different ways a hash can be expressed in Rails 3.2 and above:

 1 <h1>Hello, Rails!</h1>
  2 <%= link_to "My Blog", posts_path %><br/>
  3 <%= link_to "My Blog again", 
        :controller => :posts, 
        :action => :index %><br/>
  4 <%= link_to "My Blog (yet) again", 
        controller: :posts, 
        action: :index %>

This is a modified snippet of the sample code from one of the Ruby on Rails Guides found here. Each ‘link_to’ statement produces the same HTML link in the page served. The first (line 2) uses a rails helper, the second the older ‘hash rocket’ form and the third the newer ‘colon on the right’ form.

Advertisements

Comments are welcome!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: