# Best way to DRY this?

**URL:** <https://www.ruby-forum.com/t/best-way-to-dry-this/103766>\
**Category:** Rails\
**Created:** [May 10, 2007, 3:58am UTC](https://www.ruby-forum.com/t/best-way-to-dry-this/103766 "2007-05-10T03:58:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [May 10, 2007, 3:58am UTC](https://www.ruby-forum.com/t/best-way-to-dry-this/103766/1 "2007-05-10T03:58:17Z")

</div>

I am super new to rails and I was wondering how could I DRY this

 \<%- for page in @pages -%\> - \<%= link\_to\_remote page.title, :url =\> { :action =\> "view\_page", :id =\> page.id} %\> \<%= link\_to\_remote (image\_tag('add')), :url =\> { :action =\> 'add\_sub', :id =\> page.id } %\> 
 \<%- if page.children -%\> 
 \<%- for subpage in page.children -%\> - \<%= link\_to\_remote subpage.title, :url =\> { :action =\> "view\_page", :id =\> subpage.id} %\> \<%= link\_to\_remote (image\_tag('delete')), :url =\> { :action =\> 'delete\_page', :id =\> subpage.id } %\>
 \<%- end -%\> 
 \<%- end -%\> \<%- end -%\>

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [May 10, 2007, 5:13am UTC](https://www.ruby-forum.com/t/best-way-to-dry-this/103766/2 "2007-05-10T05:13:20Z")

</div>

Vincent, maybe I’m being dense here, but I’m not sure where you’re  
repeating  
yourself.

On 5/9/07, Vincent F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> ‘add\_sub’, :id =\> page.id } %\>  
>   
> \<%- end -%\>  
> \<%- end -%\>
> 
> >

–  
Terry (TAD) Donaghe  
[http://tadspot.tumblr.com](http://tadspot.tumblr.com)

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [May 10, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/best-way-to-dry-this/103766/3 "2007-05-10T18:01:42Z")

</div>

Vincent, while I agree that you have some logic in your view, I’m not  
sure  
that it’s too much. I don’t see where you’d need to pare something  
down,  
but maybe someone else will. It looks fine to me.

On 5/9/07, Vincent F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On 5/9/07, _Vincent F._ \< removed\_email\_address@domain.invalid  
> > \<%= link\_to\_remote (image\_tag(‘add’)), :url =\> {  
> > \<%= link\_to\_remote (image\_tag(‘delete’)), :url

–  
Terry (TAD) Donaghe  
[http://tadspot.tumblr.com](http://tadspot.tumblr.com)

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [May 10, 2007, 6:08am UTC](https://www.ruby-forum.com/t/best-way-to-dry-this/103766/4 "2007-05-10T06:08:22Z")

</div>

Terry, Sorry I used the wrong wording. I think what I meant to say was I  
seem to have alot of logic in my view I would like a more elegant way to  
achieve what I have here. a helper? im am not sure.

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [May 10, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/best-way-to-dry-this/103766/5 "2007-05-10T18:22:00Z")

</div>

On May 9, 8:56 pm, Vincent F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <ul>
> 
> ```

Looks pretty good, but if things feel ugly, you might moving the  
inside of each loop intto partials:

So your whole template is reduced to:

 \<%= render :partial =\> 'page', :collection =\> @pages %\>

and then \_page.rhtml would be:

- \<%= link\_to\_remote page.title, :url =\> { :action =\> "view\_page", :id =\> page.id} %\> \<%= link\_to\_remote (image\_tag('add')), :url =\> { :action =\> 'add\_sub', :id =\> page.id } %\>

\<%= render :partial =\> ‘subpage’, :collection =\> page.children if  
page.children %\>

and finally \_subpage.rhtml would be:

- \<%= link\_to\_remote subpage.title, :url =\> { :action =\> "view\_page", :id =\> subpage.id} %\> \<%= link\_to\_remote (image\_tag('delete')), :url =\> {:action =\> 'delete\_page', :id =\> subpage.id } %\>

More files, but each one is simple. Just food for thought.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software
