Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Simple file linking question
4 Posts
Simple file linking question

Hello all,

I'm a newcomer to Ruby and Rails, freshly abandoning years of ASP development. I'm trying to figure out how to perform a server-side include with RoR.

I want to include an external HTML file on every layout page so that when the site's navigation requires modification, I only have to update one file instead of all the layouts.

The layouts support tags such as 'stylesheet_link_tag' or 'javascript_link_tag', but I cannot find anything similar for HTML.

I was able to use something like this:

...but the problem there is that the path breaks once you get into a page that requires a variable (like /show or /edit).

There's got to be an easy way to do this that I've overlooked. Anyone?

Thanks, JR

Actually, you can scratch this question. The File.open code does work, and in all cases. It's my CSS that is wrong by defining links to images with relative paths. Silly me...


you should render a partial in this case - it looks like:

'some/dir/my_file' %>

which will result in the content of #{RAILS_ROOT}/app/views/some/dir/my_file.html.erb inserted into the file you called the rendering from.

Actually rendering a partial is much more powerful than simple including a snippet of HTML code, since you can provide different parameters (e.g. which layout should be used to render the partial, local variables via the 'local' hash, and other interesting options.)

Hope this helps - google for 'rails partials' for more info!

Cheers, Peter

And to take the whole thing a step further checkout the the content_for helper that lets you yield html view code into different parts of your layouts!

4 Posts
Login to add your message