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 -- Posted via http://www.ruby-forum.com/.
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, > >RayThis 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
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 -- Posted via http://www.ruby-forum.com/.
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 :-( -- Alex
> @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
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.netI 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 -- Posted via http://www.ruby-forum.com/.
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
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:> 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 > _______________________________________________ > 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!
Ray Fix wrote:> 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? >@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