# Partials and a question of correct form

**URL:** <https://www.ruby-forum.com/t/partials-and-a-question-of-correct-form/193683>\
**Category:** Rails\
**Created:** [August 22, 2010, 6:57am UTC](https://www.ruby-forum.com/t/partials-and-a-question-of-correct-form/193683 "2010-08-22T06:57:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 22, 2010, 6:57am UTC](https://www.ruby-forum.com/t/partials-and-a-question-of-correct-form/193683/1 "2010-08-22T06:57:09Z")

</div>

I’m new to Rails and trying to figure out the correct way to handle  
dynamic data in a partial. The partial in question will be used to  
display a google ad. The html for this ad will differ based upon the  
user (do they pay to not see ads, what the format/style of the ad will  
look like needs to match the look and feel they have configured for  
their profile, etc.).

I was looking to place an ad in a view as follows:

\<%= render “shared/ad\_banner”, :ad\_type =\> “120x90”%\>

where the ad type basically specifies what size ad the ad\_banner  
partial needs to render. The different html for the ads are stored in  
a database. My question is can/should the partial do a lookup to  
figure out what ad html to render based upon the ad\_type passed in  
(along with information about the currently logged in user)? Or should  
the actual lookup occur in the controller and the html be passed into  
the partial? My first thought was it would be cleaner if I had the  
partial do the lookup since the controller would not have to have any  
knowledge about what size ad needs to be rendered, this information  
was basically embedded in the view.

However, when trying to figure out how to make the partial do the  
lookup I started wondering if this was bad form based upon the lack of  
examples I saw for this type of approach (all the examples seemed to  
be passing the data in).

Any guidance would greatly be appreciated.

Chris

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 22, 2010, 10:03am UTC](https://www.ruby-forum.com/t/partials-and-a-question-of-correct-form/193683/2 "2010-08-22T10:03:13Z")

</div>

On 22 August 2010 05:56, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, when trying to figure out how to make the partial do the  
> lookup I started wondering if this was bad form based upon the lack of  
> examples I saw for this type of approach (all the examples seemed to  
> be passing the data in).

My thought would be that the view code should preferably not be  
looking up stuff. The controller should provide (via @ variables) all  
the data that it needs. However, if you are talking about the size of  
something on display this should certainly not be defined in the  
controller. The controller should not say ‘display this at a certain  
size’, but should provide whatever data the view needs in order that  
it can decide the size. In fact generally the size would be in css  
with the view just setting up classes so even the view does not have  
to know the size or layout.

Colin

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 22, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/partials-and-a-question-of-correct-form/193683/3 "2010-08-22T15:09:21Z")

</div>

you should keep logic out of views partials.

here are a couple different ways to accomplish this

1. create a helper function

# app/helpers/application\_helper.rb

def render\_ad\_html  
if not logged\_in? or current\_user.show\_ads?  
ad = Ad.find\_by\_controller\_name( params[:controller] ) #  
render :partial =\> “shared/ad\_#{ad.dimensions}”, :ad =\> ad  
end  
end

then in your application layout file (or in any view) you can call  
\<%= render\_ad\_html %\>

1. create a before\_filter (\*preferred)

# app/controllers/application\_controller.rb

class ApplicationController \< …  
def setup\_html\_ads  
if logged\_in? and current\_user.show\_ads?  
@ad = Ad.find\_by\_controller\_name( params[:controller] )  
end  
end

you can load the before\_filter in whatever controllers you want to  
show ads  
#app/controllers/user\_controller  
class UserController \< ApplicationController  
before\_filter :setup\_html\_ads  
end

and render them in your views if you have an @ad object  
\<%= render :partial =\> “shared/ad” if @ad %\>

either way will work. but it’s best to keep your views as dumb as  
possible. for instance, if you want to show the adverts in your rss  
stream or in emails…  
you don’t want to worry about the conditional logic contained in the  
partial… since you just want the html.

cheers,  
sean  
[http://seanbehan.com](http://seanbehan.com)
