Forum: Ruby on Rails help remove duplication in my app

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2005-12-27 23:07
(Received via mailing list)
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:

<div class="box">
   <img src="/images/content-top2.png" class="border" />
   <div class="visible">

     # content inserted here

   </div>
   <img src="/images/content-bottom2.png" class="border" />
</div>

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 ~
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-12-27 23:13
(Received via mailing list)
On Tue, 2005-12-27 at 17:04 -0500, J. Ryan Sobol wrote:
>    </div>
>
> ~ ryan ~

Ryan,

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

It will look like this:
<div class="box">
   <img src="/images/content-top2.png" class="border" />
   <div class="visible">
   <%= content %>
   </div>
   <img src="/images/content-bottom2.png" class="border" />
</div>


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
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-27 23:16
(Received via mailing list)
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:

<div class="box">
   <img src="/images/content-top2.png" class="border" />
   <div class="visible">

     <%= @content_for_layout %>

   </div>
   <img src="/images/content-bottom2.png" class="border" />
</div>

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

Sorry if I misunderstood you.

Thanks.
Steven
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2005-12-27 23:19
(Received via mailing list)
Jamie,

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

~ ryan ~
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2005-12-27 23:25
(Received via mailing list)
Steven,

Don't be sorry.  :)

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 ~
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2005-12-27 23:37
(Received via mailing list)
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 ~
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2005-12-27 23:40
(Received via mailing list)
Nevermind, I'm an idiot.

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

~ ryan ~
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2005-12-28 05:59
(Received via mailing list)
On 12/27/05, J. Ryan Sobol <ryansobol@gmail.com> wrote:
>
> >> <div class="box">
> >> as each posts (think blog),  needs to be wrapped in this html.
> > You want partials.  create a file
> >
> >
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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 Maxwell
Chief Technologist
E Factor Media // FN Interactive
kyle@efactormedia.com
1-866-263-3261
This topic is locked and can not be replied to.