1

I am assuming this applies to other things as well, but this where I've noticed it the most in the tutorials I've gone through so far. Basically, what is the difference between:

<%= render :partial => "shared/warning" %>

and

<%= render partial: "shared/warning" %>
webster
  • 3,902
  • 6
  • 37
  • 59
Taylor Huston
  • 1,104
  • 3
  • 15
  • 31
  • Effectively no difference. Hash rockets are an older (pre-Rails 4) syntax, but still supported. If you're just now picking up Rails, stick with the newer syntax, since most of the [current examples and guides](http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials) will use that syntax. – MarsAtomic Jun 19 '15 at 03:49
  • Ahh okay. I'd seen it both ways, always seemed to do the same thing, just wanted to make sure there wasn't some odd nuanced difference I was unaware of. – Taylor Huston Jun 19 '15 at 03:51
  • possible duplicate of [Ruby: Colon before vs after](http://stackoverflow.com/questions/24661857/ruby-colon-before-vs-after) – Arup Rakshit Jun 19 '15 at 04:39

3 Answers3

6

The syntax for a Hash literal in ruby is:

{ key => value }

The key can be any object, including a Symbol, eg.

{ :foo => "bar" }

Using a symbol for the keys in a hash became so popular, and so idiomatic in ruby that in ruby 1.9 an optional syntax was added for a hash created with symbol keys, and from there on the following is precisely equivalent to the above:

{ foo: "bar" }

Update

Further to your specific use case, ruby also allows you to drop the {}s when passing the Hash as an argument to a method (as well as being able to drop the ()s), so the following are equivalent:

foobar( { foo: "bar" } )
foobar( foo: "bar" )
foobar foo: "bar"
foobar :foo => "bar"
smathy
  • 26,283
  • 5
  • 48
  • 68
1

As per I know , both are same . And last one you mentioned is recommended .

Mahabub Islam Prio
  • 1,075
  • 11
  • 25
0

Both does the same thing and the second one is recommended.

You could also use render instead of render :partial

<%= render "shared/warning" %>

render is a shorthand for render :partial. But render will not accept additional local variables for the partial via :locals hash, you need to use render :partial as following for that:

<%= render partial: "shared/warning", locals: { my_var: 'Hi!' } %>

In the shorthand syntax you can pass in local variables like this:

<%= render "shared/warning", my_var: "Hi!" %>

webster
  • 3,902
  • 6
  • 37
  • 59
  • 1
    Actually, even with the shorthand syntax you can pass in local variables, just do them right in the arg list (rather than in a `:locals` hash, eg. `= render "shared/warning", my_var: "Hi!"` – smathy Jun 19 '15 at 16:14