# Code Cleaning

**URL:** <https://www.ruby-forum.com/t/code-cleaning/184849>\
**Category:** Rails\
**Created:** [March 9, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849 "2010-03-09T14:01:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/1 "2010-03-09T14:01:10Z")

</div>

Hi there,

I’m kind of a newbie on RoR and on this group and I was wondering if  
someone could help to clean this bit of code:

\<%= link\_to\_remote (image\_tag(“hide.png”, :size=\>“16x16”),  
:url=\>"/message/#{message.id}/hide",  
:method=\>“post”,  
:before=\>“Effect.Fade(this.parentNode.parentNode, {duration:  
0.5});  
Effect.BlindUp(this.parentNode.parentNode, {duration:  
0.5})”)  
%\>  
\<%= link\_to\_remote (image\_tag(“top.png”, :size=\>“16x16”),  
:url=\>"/message/#{message.id}/top",  
:method=\>“post”,  
:before=\>“Effect.Fade(this.parentNode.parentNode, {duration:  
0.5});  
Effect.BlindUp(this.parentNode.parentNode, {duration:  
0.5})”)  
%\>  
\<%= link\_to\_remote (image\_tag(“bottom.png”, :size=\>“16x16”),  
:url=\>"/message/#{message.id}/bottom",  
:method=\>“post”,  
:before=\>“Effect.Fade(this.parentNode.parentNode, {duration:  
0.5});  
Effect.BlindUp(this.parentNode.parentNode, {duration:  
0.5})”)  
%\>

As you can see I have some kind of options that do repeat themselves.  
My idea was to find a way to describe them for once and use them  
repeatedly.  
Any thoughts on how to achieve this,

Thanks in advance,

Christophe

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/2 "2010-03-09T14:19:19Z")

</div>

> I’m kind of a newbie on RoR and on this group and I was wondering if  
> someone could help to clean this bit of code:

How about something as simple as:

\<% for image\_filename in [“hide.png”, “top.png”, “bottom.png”] %\>  
\<%= link\_to\_remote (image\_tag(image\_filename, :size=\>“16x16”),  
:url=\>"/message/#{message.id  
}/hide",  
:method=\>“post”,

:before=\>"Effect.Fade(this.parentNode.parentNode, {duration:  
0.5});

Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})")  
%\>  
\<% end %\>

Alternatively, make a helper method and call that each time.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/3 "2010-03-09T14:32:12Z")

</div>

Thanks,  
That’s a good start.

Any advice where to start looking for how to make a helper method ?

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/4 "2010-03-09T15:10:16Z")

</div>

On 9 March 2010 13:30, Christophe D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Thanks,  
> That’s a good start.
> 
> Any advice where to start looking for how to make a helper method ?

I’d recommend Agile Web D. with Rails (Pragmatic Programmers  
book).  
I looked online but couldn’t see a lot that was easy to learn from.

Basically a helper method is a method that is available in your views  
and  
(can be) used for generating markup that would be repetitive otherwise.

For example you could have something this:

application\_helper.rb:  
module ApplicationHelper  
def christophe\_link\_action(image\_filename)  
link\_to\_remote (image\_tag(image\_filename, :size=\>“16x16”),

```
                                           :url=>"/message/#{message.id

```

}/hide",  
:method=\>“post”,

:before=\>"Effect.Fade(this.parentNode.parentNode, {duration:  
0.5});

Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})")

end  
end

Then in your view just do:

\<%= christophe\_link\_action(“foo.png”) %\>  
\<%= christophe\_link\_action(“bar.png”) %\>

Does that make things any clearer? If not, I’d recommend the book I  
mentioned earlier (or post back with questions).

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/5 "2010-03-09T15:24:19Z")

</div>

Thanks,  
This is good concise explanation.  
I do have the book you suggests, but haven’t had the time to go deep  
enough in it.  
Furthermore, I went back to it to check what it says about helper  
creation, but didn’t quite understood

Cheers,  
Christophe  
Le 9 mars 2010 Ã 15:08, Andy J. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/6 "2010-03-09T15:21:58Z")

</div>

Only thing to note it that the :url parameter changes on each line (it  
does  
not always end with ‘hide’!)

The fact that it is the same as the image name should be treated as an  
unfortunate coincidence. The christophe\_link\_action() method should take  
two  
parameters, one for the image file name and one for the url (or at least  
the  
last part of it).

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 4:04pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/7 "2010-03-09T16:04:42Z")

</div>

Agreed. I didn’t notice that in the OP’s code.

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [March 9, 2010, 6:36pm UTC](https://www.ruby-forum.com/t/code-cleaning/184849/8 "2010-03-09T18:36:52Z")

</div>

Christophe wrote:

> Hi there,
> 
> I’m kind of a newbie on RoR and on this group and I was wondering if  
> someone could help to clean this bit of code:
> 
> \<%= link\_to\_remote (image\_tag(“hide.png”, :size=\>“16x16”),  
> :url=\>“/message/#{message.id}/hide”,  
> :method=\>“post”,  
> :before=\>“Effect.Fade(this.parentNode.parentNode, {duration:  
> 0.5});  
> Effect.BlindUp(this.parentNode.parentNode, {duration:  
> 0.5})”)  
> %\>  
> \<%= link\_to\_remote (image\_tag(“top.png”, :size=\>“16x16”),  
> :url=\>“/message/#{message.id}/top”,  
> :method=\>“post”,  
> :before=\>“Effect.Fade(this.parentNode.parentNode, {duration:  
> 0.5});  
> Effect.BlindUp(this.parentNode.parentNode, {duration:  
> 0.5})”)  
> %\>  
> \<%= link\_to\_remote (image\_tag(“bottom.png”, :size=\>“16x16”),  
> :url=\>“/message/#{message.id}/bottom”,  
> :method=\>“post”,  
> :before=\>“Effect.Fade(this.parentNode.parentNode, {duration:  
> 0.5});  
> Effect.BlindUp(this.parentNode.parentNode, {duration:  
> 0.5})”)  
> %\>
> 
> As you can see I have some kind of options that do repeat themselves.  
> My idea was to find a way to describe them for once and use them  
> repeatedly.  
> Any thoughts on how to achieve this,

Well, one thing to do: take the JavaScript out of your ERb files and put  
it in a separate JavaScript file. This has many advantages, among them  
the fact that it’s easier to reduce repetition.

> Thanks in advance,
> 
> Christophe

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
