# How to DRY REST admin path in URLs?

**URL:** <https://www.ruby-forum.com/t/how-to-dry-rest-admin-path-in-urls/130558>\
**Category:** Rails\
**Created:** [February 29, 2008, 11:47am UTC](https://www.ruby-forum.com/t/how-to-dry-rest-admin-path-in-urls/130558 "2008-02-29T11:47:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 29, 2008, 11:47am UTC](https://www.ruby-forum.com/t/how-to-dry-rest-admin-path-in-urls/130558/1 "2008-02-29T11:47:21Z")

</div>

Hi everyone…

For almost all of my objects I put a series of classic REST admin links  
in views. For example, for a ‘user’, I have

\<%= link\_to “Show”, user\_path(user) %\>  
\<%= link\_to “Edit”, edit\_user\_path(user) %\>  
\<%= link\_to “Destroy”, user\_path(user), :confirm =\> “Are you sure”,  
:method =\> :delete %\>

I am tired of writing those links down over and over and look for a way  
to DRY it a little bit.  
I am thinking about a helper method allowing me to write a simple

\<%= admin\_links(user) %\>

but I don’t know how to dynamically write the user\_path, edit\_user\_path  
and so on?

An idea anyone?

In advance, thank you very much!

Yannis

---

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 29, 2008, 2:03pm UTC](https://www.ruby-forum.com/t/how-to-dry-rest-admin-path-in-urls/130558/2 "2008-02-29T14:03:59Z")

</div>

Hi Yannis,

You could define a helper method as follows:

def admin\_links(user)  
“\<%= link\_to “Show”, user\_path(#{user}) %\>  
\<%= link\_to “Edit”, edit\_user\_path(#{user}) %\>  
\<%= link\_to “Destroy”, user\_path(#{user}), :confirm =\> “Are you  
sure”, :method =\> :delete %\>”  
end

In your controller, you would probably define the @user(s) variable  
containing either a single user or an array of users. So, in your  
view, you would call:

\<%= admin\_links(@user) %\>

or if iterating through an array of users:

\<%= admin\_links(user) %\>

This is untested and I think the helper method might need a little  
refining but it shows how to pass the user variable through to  
generate the correct links.

Regards

Robin

On Feb 29, 10:47 am, Yannis J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 29, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/how-to-dry-rest-admin-path-in-urls/130558/3 "2008-02-29T14:09:09Z")

</div>

Yannis J. wrote:

> I am tired of writing those links down over and over and look for a way  
> to DRY it a little bit.  
> I am thinking about a helper method allowing me to write a simple
> 
> \<%= admin\_links(user) %\>
> 
> but I don’t know how to dynamically write the user\_path, edit\_user\_path  
> and so on?

The following works (for me) in a simple test:

def admin\_links element  
text = "

 “+link\_to(‘Show’, element)+” \n"  
text += " “+link\_to(‘Edit’,  
self.send(“edit\_#{element.class.to\_s.downcase}\_path”, element))+” \n" text += " "+link\_to('Destroy', element, :confirm =\> 'Are you sure?', :method =\> :delete)+" \n" text end

There may be plugins or gems that already do this, but I haven’t had a  
look…

I don’t really like the way I have constructed the edit\_XXX\_path method  
name, in particular because it won’t work for named routes, but it works  
for simple standard routes and is a start…

---

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 29, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/how-to-dry-rest-admin-path-in-urls/130558/4 "2008-02-29T14:15:16Z")

</div>

Robin F. wrote:

> Hi Yannis,
> 
> You could define a helper method as follows:
> 
> def admin\_links(user)  
> “\<%= link\_to “Show”, user\_path(#{user}) %\>  
> \<%= link\_to “Edit”, edit\_user\_path(#{user}) %\>  
> \<%= link\_to “Destroy”, user\_path(#{user}), :confirm =\> “Are you  
> sure”, :method =\> :delete %\>”  
> end
> 
> In your controller, you would probably define the @user(s) variable  
> containing either a single user or an array of users. So, in your  
> view, you would call:
> 
> \<%= admin\_links(@user) %\>
> 
> or if iterating through an array of users:
> 
> \<%= admin\_links(user) %\>
> 
> This is untested and I think the helper method might need a little  
> refining but it shows how to pass the user variable through to  
> generate the correct links.
> 
> Regards
> 
> Robin
> 
> On Feb 29, 10:47ï¿½am, Yannis J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

@Robin  
Thank you for your answer.  
I think my question was not really clear, sorry for that.  
In fact I would like to have my admin\_links(object) method to accept any  
object having resources.  
Then I would be able to admin\_links(object) and have the links returned  
as ‘object’_path(object) or edit_’object’\_path(object).

@Mark  
Again, thank you very, much that’s exactly what I was looking for…
