Taylor Huston
Taylor Huston

Reputation: 1140

Difference in ways to call partials?

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" %>

Upvotes: 1

Views: 65

Answers (3)

webster
webster

Reputation: 4012

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!" %>

Upvotes: 0

smathy
smathy

Reputation: 27961

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"

Upvotes: 7

Mahabub Islam Prio
Mahabub Islam Prio

Reputation: 1085

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

Upvotes: 1

Related Questions