Forum: Ruby on Rails yield vs @content_for_layout, etc..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2af8daf170a3ed693e577a9471ea1f10?d=identicon&s=25 Ray Fix (rayfix)
on 2006-01-23 15:10
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?

That's all I can think of for now.
Thanks,

Ray
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-01-23 15:31
(Received via mailing list)
At 1/23/2006 09:10 AM, you wrote:
>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?
>
>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.

-Rob
C7669e8b5676f61fdf202230cbcf72d8?d=identicon&s=25 Mikkel Bruun (mikkel)
on 2006-01-23 15:39
Rob Biedenharn wrote:

1. Dunno

2. 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

3. 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...

Mikkel
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-23 15:47
(Received via mailing list)
Mikkel Bruun wrote:
> Rob Biedenharn wrote:
>
> 1. 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.

> 2. 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 :-(
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-01-23 16:13
(Received via mailing list)
> @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 Olson
http://techno-weenie.net
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-23 18:21
Rick Olson wrote:
>> @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 Olson
> http://techno-weenie.net

I find yield nicer because it really works like:

File.open 'file' do
...
end

You're inserting something in the middle. (in this case:

h = fopen('file');
yield
close(h)
)

Jules
5b9fe87ec1faa67a4599782930f45ec9?d=identicon&s=25 Sam Stephenson (Guest)
on 2006-01-23 19:37
(Received via mailing list)
On 1/23/06, Rick Olson <technoweenie@gmail.com> wrote:
> > @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.

--
sam
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-23 22:40
(Received via mailing list)
Sam,

I ran into the 'seperate the navigation from layout files' as well and
solved
it with this in my layout file:

<div id="navright">
	<%= render :partial => "shared/navright" %>
</div>

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 Stephenson 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.
>
> --
> sam
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-01-24 07:58
(Received via mailing list)
Ray Fix wrote:
>
> 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.
> That's all I can think of for now.
> Thanks,
>
> Ray
>
>

-- stefan

--
For rails performance tuning, see: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
This topic is locked and can not be replied to.