I am relatively new to RoR and Ruby for that matter. A few questions
for the pros:
Question 1:
In the 5min ajax video the presenter uses
<%= yield %> in his layout.
In the agile book, @content_for_layout is used.
Since, yield is an actual Ruby construct, is it more efficient and
preferred? What is the difference?
Question 2:
Links are often of the form :action => ‘some_action’ However, I have
also seen :action => :some_action Which form is preferred? Why does
this work? How are they different?
Question 3:
Links often contain a link to an object. :id => @object which is
really short for :id => @object.id. How is that conversion able to take
place. Which form is preferred? Is it more efficient to write it out?
Links often contain a link to an object. :id => @object which is
really short for :id => @object.id. How is that conversion able to take
place. Which form is preferred? Is it more efficient to write it out?
That’s all I can think of for now.
Thanks,
Ray
This is the only one that I can answer because I asked this of Dave
Thomas last August at a presentation. The magic with “:id => @object” looks up the primary key in ActiveRecord which is probably
called ‘id’, but not necessarily. This is one of those places where
you’d use @object.id even if your primary key was overridden to a
different name and the idiom of leaving off the .id makes a bit of sense
here.
In Rails (not ruby) Strings and symbols are interchangeable.
if you have 4 strings containing “index”, they will take up “index”*4
space in memory…Symnbols on the other hand are unique, so 4 :index
will only take consume 1 index chunk of memory
models has a paramter called to_param, which default return the
id…its called whenever you construct a param from a model, not using
the model.id notation.
Using the delegated approach makes most sense…
Dunno @content_for_layout is better, because you can have @content_for_anything_you_like, and as many @content_for_s as your
layout requires, like for navigation areas, or ad-bars, or whatever.
Yield assumes (I think) that there’s only going to be injected content
at a single point in the layout.
In Rails (not ruby) Strings and symbols are interchangeable.
Not quite. There are a few places that will take one but not the other,
and vice versa. It’s generally true, but occasionally not. It’s the
occasionallies that trip me up
@content_for_layout is better, because you can have @content_for_anything_you_like, and as many @content_for_s as your
layout requires, like for navigation areas, or ad-bars, or whatever.
Yield assumes (I think) that there’s only going to be injected content
at a single point in the layout.
Actually, I believe you can do <%= yield :nav %> to print out <%= @content_for_nav %>. As for which is better, I’m not really sure.
@content_for_layout is better, because you can have @content_for_anything_you_like, and as many @content_for_s as your
layout requires, like for navigation areas, or ad-bars, or whatever.
Yield assumes (I think) that there’s only going to be injected content
at a single point in the layout.
Actually, I believe you can do <%= yield :nav %> to print out <%= @content_for_nav %>. As for which is better, I’m not really sure.
@content_for_layout is better, because you can have @content_for_anything_you_like, and as many @content_for_s as your
layout requires, like for navigation areas, or ad-bars, or whatever.
Yield assumes (I think) that there’s only going to be injected content
at a single point in the layout.
Actually, I believe you can do <%= yield :nav %> to print out <%= @content_for_nav %>. As for which is better, I’m not really sure.
Rick is correct. yield is shorter and more idiomatic than @content_for_layout, but internally it just wraps the @content_for_*
instance variables, so in the end it’s just a matter of taste.
I ran into the ‘seperate the navigation from layout files’ as well and
solved
it with this in my layout file:
<%= render :partial => "shared/navright" %>
And with a directory app/views/shared, and file in there called
_navright.rhtml
Your suggestion with <%= @content_for_nav %> sounds good as well. How
would
the ‘nav’ part in @content_for_nav used/expanded. With:
layout ‘mylay’
in your controller. You can set the layout that it will choose. Would:
nav ‘mynav’
be used in you controller when having: <%= @content_for_nav %> in your
layout?
And where will it look voor the file mynav.rhtml?
Thanx,
Gerard.
On Monday 23 January 2006 19:34, Sam S. tried to type something
like:
Rick is correct. yield is shorter and more idiomatic than @content_for_layout, but internally it just wraps the @content_for_*
instance variables, so in the end it’s just a matter of taste.
Since, yield is an actual Ruby construct, is it more efficient and
preferred? What is the difference?
@content_for_layout is probably more efficient, but I haven’t measured
this yet.
Question 2:
Links are often of the form :action => ‘some_action’ However, I have
also seen :action => :some_action Which form is preferred? Why does
this work? How are they different?
I prefer “some_action”, but this is just personal taste.
Question 3:
Links often contain a link to an object. :id => @object which is
really short for :id => @object.id. How is that conversion able to take
place. Which form is preferred? Is it more efficient to write it out?
The route generation module calls :to_param on expr, if you pass :key =>
expr and expr evaluates to something that responds to to_param. For AR
models, model_instance.to_param is equivalent to model_instance.id,
unless you overwrite it.