# Noob learning DRY

**URL:** <https://www.ruby-forum.com/t/noob-learning-dry/174229>\
**Category:** Rails\
**Created:** [August 22, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229 "2009-08-22T23:20:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/1 "2009-08-22T23:20:21Z")

</div>

I am very new to RoR (this is my second day). I have started my first  
application, and want to follow best practices as much as possible. I  
created a select box that lists all the items in a table column on a  
new.html.erb page. I then realized I needed to create one on the edit  
page. I want to use this select box on several other pages as well.

Trying to keep with D.R.Y. I am wondering if there is a better way to  
do this, instead of putting this statement on every page.

\<%= select(“note”, “project\_id”, Project.find(:all).collect {|p|  
[p.name, p.id]}) %\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 3:28am UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/2 "2009-08-23T03:28:34Z")

</div>

Hi,

Have a look at partials. That’s where you can call the same snippet of  
render code from several views. Also, best practice seems to indicate  
you  
should put the find in the controller, not in the view.

Cheers  
Simon

On Sun, 23 Aug 2009 04:10:45 +0800, removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 3:52am UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/3 "2009-08-23T03:52:00Z")

</div>

On Sat, Aug 22, 2009 at 6:48 PM, removed\_email\_address@domain.invalid \<  
removed\_email\_address@domain.invalid\> wrote:

> > should put the find in the controller, not in the view.
> > 
> > > created a select box that lists all the items in a table column on a  
> > > new.html.erb page. I then realized I needed to create one on the edit  
> > > page. I want to use this select box on several other pages as well.
> > 
> > > Trying to keep with D.R.Y. I am wondering if there is a better way to  
> > > do this, instead of putting this statement on every page.
> > 
> > > \<%= select(“note”, “project\_id”, Project.find(:all).collect {|p|  
> > > [p.name, p.id]}) %\>

Hi, I would recommend using the guides at the following location:

> **[rails.info](http://guides.rails.info)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 3:49am UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/4 "2009-08-23T03:49:04Z")

</div>

Thanks guys. I will look more into partials and also try to move my  
find into the controller. I appreciate the help!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 6:29am UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/5 "2009-08-23T06:29:23Z")

</div>

There are a few core practices you can follow with view templates and  
attempting a DRY concept.

Partials are fine if you have a lot of repeating HTML in many of your  
views because you can use a partial for table structures etc. As an  
example, if you are creating a large table and that table needs to be  
used in several views within the same controller, you can use a partial  
for the other views.

However, when working with actual code, I like to work with helpers.  
The best practice is not to include too much code functionality in your  
views but move those to a helper if you find yourself needing the same  
one over and over.

You should follow these basic things:

Views are meant for html and all things that represent the display your  
users see. Your controllers are the director that talks to your models  
and to your views. When you need a database call, the controller talks  
to the model and the model retrieves the data and hands it back to the  
controller, who then hands it back to the view.

Don’t make the mistake of trying to skip the MVC architecture in your  
project. If you form bad habits they are hard to break.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 9:11am UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/6 "2009-08-23T09:11:00Z")

</div>

I think the only thing I would change from the code is to place  
Project.find(:all).collect {|p| [p.name, p.id]}) in Project model

def self.to\_options  
find(:all).map { |p| [p.name, r.id] }.unshift([‘Choose a  
project:’, ‘’])  
end

So then I’ll just use it like this

\<%= select(“note”, “project\_id”, Project.to\_options) %\>

So you don’t have meaningless helper, it still DRY and still follows  
MVC

Cheers!  
Arzumy

On Aug 23, 12:29 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 4:08am UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/7 "2009-08-23T04:08:24Z")

</div>

Hi Greg,

On Sat, 2009-08-22 at 13:10 -0700, removed\_email\_address@domain.invalid wrote:

> I am very new to RoR (this is my second day).

Welcome!

> [p.name, p.id]}) %\>  
> Short answer… maybe. You could refactor it into a partial but a  
> one-liner partial usually doesn’t make much sense. One part of the DRY  
> rationale is about minimizing lines of code in the code base. That’s  
> goodness, but with one-liners you’re actually increasing the number of  
> lines of code by one and the size question always needs to be balanced  
> against the performance hit of initializing another class. Don’t get  
> too anal with DRY. Opinions vary but, IMO, the real value of DRY is to  
> minimize the number of places you have to go to make changes. So, if  
> you think this select is likely to be used in a lot of places and is  
> likely to change, then you should consider moving it to a partial more  
> strongly than if it’s not likely to change.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/8 "2009-08-23T13:24:26Z")

</div>

2009/8/23 Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I tend to agree with this - one-liners typically go into helpers in my  
> code, while anything longer is factored out into a partial. It can  
> also be helpful to have a helper which crunches some parameters and  
> then calls a partial. But that style is partly driven by writing a lot  
> of DRYML…
> 
> As for the suggestions for to\_options - that can be useful sometimes,  
> but it’s getting (IMHO) dangerously close to sticking controller/view  
> code in the model (especially the prompt text).

I disagree here, I would contend that there is nothing wrong with have  
a model method which performs the action “Get a set of valid options”.  
_How_ to display it - as a select box, checkboxes, etc and how it is  
formatted is up to the view, but the model should decide which options  
are provided. I also think it is ok to call this from the view in  
this situation rather than from the controller to pass to the view.  
The controller does not need to know that the view wants to display a  
list for the user to select from.

Colin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/noob-learning-dry/174229/9 "2009-08-23T13:03:57Z")

</div>

I tend to agree with this - one-liners typically go into helpers in my  
code, while anything longer is factored out into a partial. It can  
also be helpful to have a helper which crunches some parameters and  
then calls a partial. But that style is partly driven by writing a lot  
of DRYML…

As for the suggestions for to\_options - that can be useful sometimes,  
but it’s getting (IMHO) dangerously close to sticking controller/view  
code in the model (especially the prompt text). Now, if you want to  
only have _some_ of the models on the menu (ie, you’re doing Model.find  
(conditions).map …) then the first bit of that definitely belongs in  
the model.

–Matt J.

On Aug 23, 12:29 am, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
