Help remove duplication in my app


#1

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://
www.hsgamedev.org and should be a good visual aide.

Any suggestions?

~ ryan ~


#2

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

~ ryan ~

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

#3

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.

Thanks.
Steven


#4

Jamie,

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

~ ryan ~


#5

Jamie,

<%= 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 ~


#6

Steven,

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 ~


#7

Nevermind, I’m an idiot.

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

~ ryan ~


#8

On 12/27/05, J. Ryan S. removed_email_address@domain.invalid wrote:

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

You want partials. create a file


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

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
removed_email_address@domain.invalid
1-866-263-3261