dynamic data included in application.rhtml?


The “how do I include this universally?” went down pretty easily once I

figured it out. Essentially, I created the controllers to access the data from the requisite models, then coded the view templates, then moved the controllers so that they belonged to > ActionController::Base

rather than > ApplicationController. Then I included the two controllers in my template by using render_controller => ‘projects’, action => ‘thumbs’. This wasn’t the most elegant solution because my

‘projects’ controller originally was used for two actions (thumbs/list, and show/display), and the two needed to be seperate (because show needed to be a class of AppController, while thumbs needed to belong to

ActionController). Then the features controller is stuck I think in the main app controller. So it’s not the most elegant way of approaching it, but I figure as I learn things I’ll go back & clean everything up

so it’s a little more fluid. I’m getting the impression that a lot of people learn the Rails this way, since the documentation is so all-over-the-place and varied.

I’m lost… Although you can extend classes directly from ActionController::Base i guess you shouldn’t follow that approach, instead of:

ActionController::Base ConcreteController01 and



you could do

+ApplicationController ±— ConcreteController01 ±— SarahsBaseController ±-- ConcreteController01

This way you are shielded against some Rails Core changes at ApplicationController’s level. For instance rails 1.2 RC introduces the following at this level ‘session :session_key => ‘_APP_NAME_session_id’’

Regarding layouts or global layouts whatever you put in them don’t necessarily have to be “static” you are not limited to decorate whatever comes <%= yield %>. its possible to replace/include other parts of these layouts.

Check the api for:

  • capture
  • content_for
  • render (with runtime parameters)

I’m working on something that have some views with googlemaps stuff, in this views there are some javascript files that must be included. I reference javascript files from each concrete view but, i want all javascript references to be on top of the page inside which are in a single site wide layout. By using content_for i’ve been able to dinamically place what, where, when i need without resorting to duplication of code.

Also as my knowledge of rails is increasing i’m using partials everywhere, started using them to render lists, or specific blocks of a page, but now i’m all for partials of partials of partials… to the point where its possible to interchange or reusing them between diferent views. More or less like a lego metaphor, a lego block is a partial, a piece its a composition of blocks/partials and so on until you work your way out to the result page.

have fun with rails



it should be:

+ApplicationController ±— ConcreteController01 ±— SarahsBaseController ±-- ConcreteController 02

you have ‘

Some header text

’ and expect ‘

Some header text

’ ? The idea is to replace it with

Some header text

My CSS does some stuff to make the first-letter invisible, then uses a first-letter.gif as a background for the header. So

Header would render

Header. I have a method defined in the AppHelper that turns h1 strings into the

appropriately-tagged h1 code, and all I need to do in my templates is type in <%= header “Header String” %> and it’s rendered correctly. I tried applying the header method to the replaced instances in my gsub

but it told me the method was undefined. (The gsub is also a method within my app helper).

def header(h1 = “”) if h1 =~ /

(.)/ and (!$1.strip.empty? and $1 != ‘<’)

"<h1 class=\"#{$1.strip[0,1].downcase}\">#{$1}#{$'}"

else h1 end end

probably there’s an easy way to get the same result.

This may sound a silly question but why do you want to inject a class name in an existing HTML content? couldn’t you get the same result by using a css selector for that h1 element Technically I could manually type out the class=“firstletter” for all

headers, but this seems like an overly clunky way of doing it (and will be redundant when I change the template), plus users won’t know to do it when modifying page contents & thus they’ll get the default thing

happening (Which is pretty unreadable.)

you’re right but i’m not meaning a default css selector to all H1 on the page. if you are outputting that content inside a block element by using something like

<%= your_content %>

with a style like .firstletter h1 { … }

you can apply that rule only to those h1 inside firstletter block without having to modify each h1. But what you want is to hide the first letter and replace it with an image i guess you have to change the h1 class, though you’ll at least 26 different styles to cover the alphabet.

I’m wondering if I can split the string into an array based on the

tags and run it through my header method then, then piece it all back together, but I was hoping that there might be a more elegant way

of doing it.

Try the above helper method and see if it suits your needs, the rexexp can be simplified if you make sure you don’t get something like


Some title


I'm lost...

YOU'RE lost? Man. I'm lost. I'm only just at the point where I know that an object is a model, or belongs to a model, or, wait, a class is an instance of an object, which means that it has instance variables, and a controller is a ... something, and then there's methods to act on objects.

Check the api for: - capture - content_for - render (with runtime parameters)

I will definitely look into this once I've got my app all running & such .... at which point I plan on going back to streamline & clean up the bits that got a little ugly/inelegant. I'm approaching this in a slightly ad-hoc manner but concepts & whatnot are gradually coming to me as I learn (and screw up).

Thanks for the tips!

def header(h1 = “”) if h1 =~ /

(.)/ and (!$1.strip.empty? and $1 != ‘<’) “<h1 class="#{$1.strip[0,1].downcase}">#{$1}#{$'}” else h1 end end

Try the above helper method and see if it suits your needs, the rexexp can be simplified if you make sure you don’t get something like


Some title

Trying to get this to work drove me totally insane for a full day. The code above didn’t work for me (I think because I needed a method that would apply to a string?) along with a whole myriad of other attempts. The Rails errors mean next to nothing to me still.

BUT I finally won out with this:

def replace_headers(text)

	text.gsub(/<h1>(.)/, '<h1 style="background-image: url(/images/caps/\+.gif);">\+')


which takes this

<h1>Design: 0. Madness: 12.</h1>

and creates this

<h1 style="background-image: url(/images/caps/D.gif);">Design: 0. Madness: 12.</h1>

in my application helper. I then threw <%= replace_headers yield %> into my application.rhtml where my controllers output their content (all site content is rendered by controllers), and now any and every h1-level header is rendered in such a manner as to use a great big image drop-cap, while retaining the data with its

formatting, which seems like the simplest/most elegant solution. I still need to figure out a way to strip leading whitespace, if there is any, and how to convert the first letter to lowercase, which would be easy if I could apply methods to the + element, but trial-and-error thinks it’s less likely (and for the time being, it’s something I can work around for the time being.

Once I have a more elegant solution worked out, I’ll post the method in case anyone would have any use for it … though it may well be the sort of thing for which only I have a burning desire!

Thanks again for all the tips & direction.


to strip the leading space right after the