# Cleaning up code, eliminating repetition

**URL:** <https://www.ruby-forum.com/t/cleaning-up-code-eliminating-repetition/175382>\
**Category:** Rails\
**Created:** [September 12, 2009, 9:42am UTC](https://www.ruby-forum.com/t/cleaning-up-code-eliminating-repetition/175382 "2009-09-12T09:42:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [September 12, 2009, 9:42am UTC](https://www.ruby-forum.com/t/cleaning-up-code-eliminating-repetition/175382/1 "2009-09-12T09:42:10Z")

</div>

Hi guys,

I am currently trying to clean up my views. As you can see in the  
example below, I’m having a new link to create a new resource  
dynamically. The controller is given by the params[:controller]. This  
way I create new action paths to all my resources with the same code.

I want now a similar thing but for the show action. The thing is that  
I’ll need the instance variable. How can I set the instance variable  
dynamically for a show action. I’ll have to add to the url\_for method  
below the ‘show’ parameter with the instance variable that comes from  
the controller (example: @user, @message, @foo).

I tried the following first, but it didn’t worked since the id is then  
processed as a string “@user”, so I get the following url: /users/  
@user. I hope you guys can help me trying to solve this one. Thanks!

url\_for(:controller =\> params[:controller], :action =\> “show”, :id =\>  
“@”+params[:controller].singularize)

 \<% link\_to url\_for(:controller =\> params[:controller], :action =\> "new") do %\> \<%= image\_tag("add.png") %\> \<%= t('toolbar.new') %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [September 12, 2009, 10:02am UTC](https://www.ruby-forum.com/t/cleaning-up-code-eliminating-repetition/175382/2 "2009-09-12T10:02:05Z")

</div>

I found this and it worked:

:id =\> instance\_variable\_get("@#{params[:controller].singularize}")

One more thing though. When I try to do the link\_to dynamically, I  
generated the link\_to urls using the url\_for method. Is there any  
other way to generate this routes dynamically without using the  
url\_for method? Thanks!

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [September 12, 2009, 1:33pm UTC](https://www.ruby-forum.com/t/cleaning-up-code-eliminating-repetition/175382/3 "2009-09-12T13:33:56Z")

</div>

Hi –

On Sat, 12 Sep 2009, elioncho wrote:

> Hi guys,
> 
> I am currently trying to clean up my views. As you can see in the  
> example below, I’m having a new link to create a new resource  
> dynamically. The controller is given by the params[:controller]. This  
> way I create new action paths to all my resources with the same code.

Do you mean you’re using the same template for all of these views? I  
can’t quite get my bearings in the sequence of events you’re  
describing.

David

–  
David A. Black, Director  
Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
Ruby/Rails training, consulting, mentoring, code review  
Book: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://www.manning.com/black2))

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [September 14, 2009, 10:01am UTC](https://www.ruby-forum.com/t/cleaning-up-code-eliminating-repetition/175382/4 "2009-09-14T10:01:43Z")

</div>

On Sat, Sep 12, 2009 at 1:00 AM, elioncho [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I found this and it worked:
> 
> :id =\> instance\_variable\_get(“@#{params[:controller].singularize}”)
> 
> One more thing though. When I try to do the link\_to dynamically, I  
> generated the link\_to urls using the url\_for method. Is there any  
> other way to generate this routes dynamically without using the  
> url\_for method? Thanks!

In

> **[ActionView::Helpers::UrlHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001597)**
>
> Provides a set of methods for making links and getting URLs that depend on the routing subsystem (see ActionDispatch::Routing).

It says that #link\_to relies on #url\_for, so you can pass the hash of  
{:controller, :action, :id} directly to it (without passing it to  
#url\_for first).

To get a string of the URL described by this controller/action/id  
combination, that’s when you use #url\_for.

I hope that answers your question.

Colin
