# Can someone help DRY a noob? thanks

**URL:** <https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124>\
**Category:** Rails\
**Created:** [April 28, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124 "2006-04-28T21:08:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 28, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124/1 "2006-04-28T21:08:53Z")

</div>

hi all

i’m trying to pass a variable name right round the block and need a hand  
to ease my aching brain.

##in my sidebar i have

- \<%= SidebarItem('catalogue') %\>
- \<%= SidebarItem('provenance') %\>
- \<%= SidebarItem('status') %\>

##linked to a helper

def SidebarItem(name)  
link\_to\_remote “#{name}”,  
:url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
end

but at the moment i have three almost identical actions in the  
controller: ‘catalogue’, ‘provenance’, and ‘status’ containg a line like

## def catalogue

```
   render (:template => 'admin/works/catalogue', :layout => false)
end

```

all of which call an RJS with same three names - ie catalogue.rjs - each  
RJS contains something like…

## if @updated

```
   page.replace_html 'EditFields', :partial => 'catalogue'
 end

```

so how can i dry it up and end up with something like this?

##helper

def SidebarItem(NAME)  
link\_to\_remote “#{NAME}”,  
:url=\> {:action =\> SOMEACTION(NAME), :id =\> @work.id, :layout =\>  
false}  
end

##action

def SOMEACTION(NAME)  
render (:template =\> ‘admin/works/NAME’, :layout =\> false)  
end

## where NAME.rjs contains

if @updated  
page.replace\_html ‘EditFields’, :partial =\> ‘NAME’  
end

TIA for help and patience with newbie questions

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 28, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124/2 "2006-04-28T21:13:11Z")

</div>

oops scratch that, i meant

hi all

i’m trying to pass a variable name right round the block and need a  
hand  
to ease my aching brain.

##in my sidebar i have

```
<li><%= SidebarItem('catalogue') %></li>
<li><%= SidebarItem('provenance') %></li>
<li><%= SidebarItem('status') %></li>

```

##linked to a helper

```
def SidebarItem(name)
       link_to_remote "#{name}",
       :url=> {:action => name, :id => @work.id, :layout => false }
end

```

but at the moment i have three almost identical actions in the  
controller: ‘catalogue’, ‘provenance’, and ‘status’ containg a line  
like

## def catalogue

```
    render (:template => 'admin/works/catalogue', :layout => false)
 end

```

all of which call an RJS with same three names - ie catalogue.rjs -  
each  
RJS contains something like…

## if @updated

```
    page.replace_html 'EditFields', :partial => 'catalogue'
  end

```

so how can i dry it up and end up with something like this?

##helper

```
def SidebarItem(NAME)
     link_to_remote "#{NAME}",
     :url=> {:action => SOMEACTION(NAME), :id => @work.id, :layout 

```

=\>  
false}  
end

##action

def SOMEACTION(NAME)  
render (:template =\> ‘admin/works/SOMERJS’, :layout =\> false)  
end

## where SOMERJS.rjs contains

```
if @updated
  page.replace_html 'EditFields', :partial => 'NAME'
end

```

TIA for help and patience with newbie questions

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 28, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124/3 "2006-04-28T23:37:12Z")

</div>

no takers?

my brain still hurts :~(

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [May 3, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124/4 "2006-05-03T21:14:32Z")

</div>

Mike, you’re a star.

D

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/can-someone-help-dry-a-noob-thanks/59124/5 "2006-05-03T18:56:32Z")

</div>

On 4/28/06, Damian leGassick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - \<%= SidebarItem('status') %\>
> 
> ##linked to a helper
> 
> def SidebarItem(name)  
> link\_to\_remote “#{name}”,  
> :url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
> end

I would take out this helper method altogether - it’s specifically  
tied to the ivar @work, so you can’t really use it in any other  
general sense. You could of course change it to:

def SidebarItem(name,ivar)  
link\_to\_remote “#{name}”, :url =\> {:action =\> name, :id =  
ivar.id, :layout =\> false }  
end

and then call it using:

SidebarItem(‘catalogue’,@work)

so that it would be applicable to more cases, but then you may as well  
just use the regular link\_to\_remote function. I only really use  
helpers where they replace a large chunk of code (not just a one  
liner), or I’ll be calling them over and over again from different  
parts of the application (which of course you could do with the second  
form I presented here, since it’s more general - but like I said, for  
a simple one liner like link\_to\_remote, you may as well keep it  
there).

In any case, why not do something like this:

myview.rhtml:

- \<%= link\_to\_remote 'catalogue', :url =\> {:action =\> my\_dispatcher, :id = @work.id, :arg =\> 'catalogue', :layout =\> false } %\>
- \<%= link\_to\_remote 'provenance', :url =\> {:action =\> my\_dispatcher, :id = @work.id, :arg =\> 'provenance', :layout =\> false } %\>
- my\_controller.rb:
