ASP.NET -> Rails Equivalent for Placeholder.Visible?

Subscribe to ASP.NET -> Rails Equivalent for Placeholder.Visible? 9 posts, 4 voices

 
Avatar Ed Laczynski 26 posts

Hey there everyone.

Does anyone know of a “best practice” Rails technique for hiding and showing things like page headings and content?

In ASP.NET, I used to do this:

<asp:Placeholder id="plHeading" Visible='<%# someExpressionThatReturnsBoolean %>' runat="server"> 
THIS IS A HEADING THAT SHOWS CONDITIONALLY 
</asp:Placeholder>

In Rails, I’ve been doing this:

<% if ( someExpression ) -%>    
HEADER
<% end -%></h2>

# OR 

<% if ( someExpression ) -%>
<%= someValue %>
<% end -%>


Is there a more elegant way to do this? When the expression that you are evaluating is long lke this: (params[:state] && @cities.total_results_count.to_i > 0 ) and you have to repeat it, it becomes somewhat cumbersome and I figured nothing should be cumbersome in Rails.

Thanks

 
Avatar Brian Eng 49 posts

Refactor the logic down to the controller or helper level.

As a simple example, put a method in your application controller helper:

def show_header
  return "THIS IS THE HEADER" if params[:state] && @cities.total_results_count.to_i > 0
end

Then, in the view/layout, simply:

<%= show_header %>

As a basic rule of thumb, keep your views clean and simple. Refactor the complexity down to helpers, controllers, and models.

Hope this helps.

 
Avatar Brian Eng 49 posts

Did I say application controller? I meant helper. Sorry.

 
Avatar Mike McClena... 19 posts

I would take Brian’s suggestion even further if your header ends up containing more than a line or two of HTML. Using blocks with your helpers can really be a nice way to handle conditionals in your views. The block allows you to insert the HTML into your view (rather than trying to build an HTML string in your helper) but moves the conditions into the helper. To expand on Brian’s suggestion, here’s what the block would look like:

def show_header(&block)
  yield if params[:state] && @cities.total_results_count.to_i > 0
end
and the view would then look like this:
<% show_header do %>
  <h1>This is the header</h1>
  <span class="tagline">And this is a small witty tagline to go with my header.</span>
<% end %>

Of course, this is a pretty trivial example but shows some of the power. I like to use block helpers in those situations where you need to spit out a collection onto the page. Your view can get really messy if you want to display some sort of notice like “You haven’t added any widgets to the bag yet!” in those situations where your collection is empty.

If I haven’t explained it well, check out these links for more info:

mike.

P.S. Nuts – the code didn’t post well. How do I format it in Beast? P.P.S. Thanks Brian – I’ve added the pre tag and it looks nice and pretty.

 
Avatar Brian Eng 49 posts

Good point Mike, I’ve found that technique useful as well. Re: your code formatting—wrap it with pre tag.

 
Avatar Ed Laczynski 26 posts

Thanks again SoR.

I am almost “done” (as we all know, that is never true) with my first production Rails App.

Here is what I’ve managed to build:

  • a industry-specific search portal / quote engine using geonames, geokit, and ym4r gems/plugins for geography/mapping and yahoo local search api for data
  • memcached for storage of web service call output
  • pound/mongrel_cluster to serve it all on a linux VPS. performance tested with httperf and profiled with ruby_prof. i am probably going to move to ngnix/mongrel_cluster based on what i’ve been reading about ngnix
  • developed in TextPad on my MacBook Pro

All this from a .NET developer who hasn’t used an Apple since 1st grade LOGO. Thank you EVERYONE for your help in getting me going here.

I am going to post up some stuff here and on the blog to explain what I did, and hopefully make it better (first thing is to implement the block strategy for “placeholders” as described by Mike). Here is the site BTW: http://tinyurl.com/38gpl5

To that point, is there any software available that I can use to publish my code, without giving everyone access to my SVN repository? i was thinking i could add a link to my rails apps (this might catch on) that other rails developers could pick up to view source code…

 
Avatar Jeff Cohen 89 posts

Congratulations, Ed! Another great success story.

Rock on.

 
Avatar Mike McClena... 19 posts

Ed,

Is there any reason why you wouldn’t want to give folks (read-only) access to your SVN?

One of the hardest parts for me to learn is the server stuff. I’d love it if you (or anyone else) could post some stuff on that. There are definitely some learning curves for the average ‘softie moving over to RoR but I’ve found that the curve is nothing compared to how much I’ve had to learn when it comes to deployment and all of the options that are available.

Good luck with the site. By the way, I just tried it and clicked on “North Dakota” on the index page – it gave me a 500 error.

mike.

 
Avatar Ed Laczynski 26 posts

No problem, I’ll see if I can figure out how to create read-only permissions for SVN.

The North Dakota page seemed to work for me, but it might be geonames (I use that to build the city list instead of keeping a local database of geography). I have to figure out how to persist that stuff to disk instead of memcached so that I don’t need to call it successfully more than once.