The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: An Explicit Locals Hash

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: An Explicit Locals Hash Posted: Sep 7, 2006 7:17 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: What's New in Edge Rails: An Explicit Locals Hash
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

With Rails components being shunned throughout the Rails world we are often left to use a set of view partials to abstract out commonly used display functionality. Sometimes this necessitates the use of variables (called ‘locals’) that may or may not be relevant in all view scenarios. For instance, if I have a partial that I use to display form text fields there may be a size parameter I want to pass in to limit the field’s size:


# my_form.rhtml
<%= render :partial => 'text_field', :locals => {:size => 5} %>

# _text_field.rhtml
<input type="text" size="<%= size %>" />

If I want the size parameter to be optional however, I could try to do this with a default value of 5:


# _text_field.rhtml
<input type="text" size="<%= size ? size : "5" %>" />

However, this fails if no size parameter is passed to the partial as size hasn’t been defined yet. With this update, however, you can query an explicit locals hash to determine the existance of a local variable:


# _text_field.rhtml
<input type="text" size="<%= locals[:size] ? size : "5" %>" />

Not a huge update, but if you’re using partials extensively (which I suspect most are) you’ve no doubt run into this issue.

tags: ,

Read: What's New in Edge Rails: An Explicit Locals Hash

Topic: Rails - Running your tests on deployment with Capistrano Previous Topic   Next Topic Topic: How to: Easy Rails deployment on Dreamhost

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use