# DRY up multiple calls link\_to\_remote?

**URL:** <https://www.ruby-forum.com/t/dry-up-multiple-calls-link-to-remote/113903>\
**Category:** Rails\
**Created:** [September 6, 2007, 1:19am UTC](https://www.ruby-forum.com/t/dry-up-multiple-calls-link-to-remote/113903 "2007-09-06T01:19:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [September 6, 2007, 1:19am UTC](https://www.ruby-forum.com/t/dry-up-multiple-calls-link-to-remote/113903/1 "2007-09-06T01:19:28Z")

</div>

I have the following code that will be repeated many times in a  
single .rthml page.

\<%=link\_to\_remote(“Test AJAX”,  
:update =\> ‘chart’,  
:url =\> url\_for(:controller =\> ‘chart’, :action =\>  
‘linear\_graph’),  
:loading =\> "Element.show(‘loading’); ",  
:complete =\> “Element.hide(‘loading’)”,  
:before =\> “this.parentNode.className = ‘myclass’;”) %\>

What is the best way to DRY this up so  
the :loading, :complete:, :before: and :update callbacks are reused? I  
want to avoid changing all instances of this call when I update the  
code. The only parameter that will change is :url

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [September 6, 2007, 2:42am UTC](https://www.ruby-forum.com/t/dry-up-multiple-calls-link-to-remote/113903/2 "2007-09-06T02:42:31Z")

</div>

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

> ```
> :before => "this.parentNode.className = 'myclass';") %>
> 
> ```
> 
> What is the best way to DRY this up so  
> the :loading, :complete:, :before: and :update callbacks are reused? I  
> want to avoid changing all instances of this call when I update the  
> code. The only parameter that will change is :url

I use something like the following:

in application\_helper.rb:  
def showing\_progress(opts={}, hide\_div = nil)  
opts.merge :loading =\> update\_page{|p| p[hide\_div].hide if  
hide\_div; p[:progress].visual\_effect(:appear, :duration =\> 0.5); },  
:complete =\> update\_page{|p| p[:progress].hide; p[hide\_div].show  
if hide\_div; }  
end

in my view.rhtml:  
\<%= link\_to\_remote(h(email.subject), showing\_progress({:url =\>  
{:action =\> ‘display\_email’, :id =\> email}}, ‘email\_content’)) %\>

Adam
