Help remove duplication in my app

I’m porting an old site of mine into Rails and I’m running into some
duplication that could / needs to be weeded out. For each of my
actions, I have to wrap the view content in the following HTML:

 # content inserted here

Unfortunately, I can’t add this snippet into the controller’s layout,
as each posts (think blog), needs to be wrapped in this html.

The existing, non-Rails implementation can be found at http:// and should be a good visual aide.

Any suggestions?

~ ryan ~

On Tue, 2005-12-27 at 17:04 -0500, J. Ryan S. wrote:

~ ryan ~


You want partials. create a file
called /app/views/controller/action/_box.rhtml

It will look like this:

<%= content %>

You can now render it like this in your other views:

<%= render :partial => ‘box’, :locals => { :content => @foo } %>

If you’re wanting to reuse this partial for many things, I believe you
can nest it, so replace @foo with another render :partial call, with
partials named _blog.rhtml, _comment.rhtml, or whatever.

  • Jamie

Why wouldn’t adding the code below to the controller layout work?
For example, assuming a controller named “blog”, you could have the
following in app>views>layouts>blog.rhtml:

 <%= @content_for_layout %>

The “<%= @content_for_layout %>” would include whatever content each
view produced.

Sorry if I misunderstood you.



I wasn’t aware of the ability to nest render(:partial => …) calls.
I’ll re-read the docs on the render method.

~ ryan ~


<%= render(:partial => ‘box’, :locals => { :content => render
(:partial => ‘post’) }) %> # works

<%= render(:partial => ‘box’, :locals => { :content => “<%= link_to
‘Back’, :action => ‘list’ %>” } ) %> # doesn’t work

I’d like to try to avoid creating a partial template file for small,
one-liners like the string in the second example. Any thoughts?

~ ryan ~


Don’t be sorry. :slight_smile:

I’m fairly certain this wouldn’t work because I need to, potentially,
wrap multiple blog-like posts per action. Take a look at the source
code for the site I posted in my first message to see what I mean.

~ ryan ~

Nevermind, I’m an idiot.

<%= render(:partial => ‘box’, :locals => { :content => link_to
(‘Back’, :action => ‘list’) } ) %>

~ ryan ~

On 12/27/05, J. Ryan S. [email protected] wrote:

as each posts (think blog), needs to be wrapped in this html.

You want partials. create a file

Rails mailing list
[email protected]

Rails mailing list
[email protected]

Not sure if the overall idea is the most elegant way to go, but

<%= render(:partial => ‘box’, :locals => { :content => “<%= link_to
‘Back’, :action => ‘list’ %>” } ) %> # doesn’t work

<%= render(:partial => ‘box’, :locals => { :content => “#{link_to
‘Back’, :action => ‘list’}” } ) %> # should work

Kyle M.
Chief Technologist
E Factor Media // FN Interactive
[email protected]valid

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs