# Dynamic mapping in controller

**URL:** <https://www.ruby-forum.com/t/dynamic-mapping-in-controller/77016>\
**Category:** Rails\
**Created:** [November 23, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/dynamic-mapping-in-controller/77016 "2006-11-23T12:41:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 23, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/dynamic-mapping-in-controller/77016/1 "2006-11-23T12:41:06Z")

</div>

Hi,

I have used scaffold to create my rails project and hence I have a  
database containing a table called “results” and when i call the “show”  
action followed by the ID it brings up a list of the relative record and  
its details.

What I want though is for if someone types in say  
localhost/results/etab-mobile that it maps dynamically to  
localhost/show/1 but keeps localhost/etab-mobile in the address bar. the  
“etab-mobile” title is found in the database under “module\_name” so can  
be pulled out for use.

I know this can be done in the routes.rb folder statically but would  
like to do it in the controller so that its not as static.

is this possible?

thanks,

Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 23, 2006, 1:13pm UTC](https://www.ruby-forum.com/t/dynamic-mapping-in-controller/77016/2 "2006-11-23T13:13:40Z")

</div>

Hi,

I think its possible only thru routes.rb, cannot be done in controller,  
As i see, if you have “link\_name” in your database for each page on  
site:  
like:  
[http://site.com/homepage](http://site.com/homepage) → redirects to /site/1  
[http://site.com/about](http://site.com/about) → /site/3  
in table of “menus” its looks  
id — link ----------- name — …  
1 homepage Welcome to Homepage  
3 about Everything about us

“link” field is used to be a “friendly” link and generated thru  
routes.rb.  
i think is simpliest way.

look at [http://eurobdc.com](http://eurobdc.com) at their links.

Cheerz

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 23, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/dynamic-mapping-in-controller/77016/3 "2006-11-23T15:43:35Z")

</div>

that sounds like the way id like to do it but im having trouble getting  
my head around how to code it:

at the moment i have

map.connect ‘etab-mobile’,  
:controller =\> ‘results’,  
:action =\> ‘show’,  
:id =\> ‘1’

but id like that :id =\>‘1’ to be loaded in from my database rather than  
statically as I have other results that id like to show with id =2 and  
id=3 and so on…

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [November 23, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/dynamic-mapping-in-controller/77016/4 "2006-11-23T16:04:12Z")

</div>

You cannot display dynamic results under the same route showing in the  
address bar, except you use POST instead of GET

Maybe that would work … someone would type :  
[http://yoursite.com/results/etab-mobile](http://yoursite.com/results/etab-mobile)  
and the route would be:  
map.connect ‘results/:module\_name’,  
:controller =\> ‘results’,  
:action =\> ‘show’

then you have params[:module\_name] available in your show action and  
can pull the correct dataset from the DB  
with find(:first, :conditions =\> [module\_name =  
?,params[:module\_name]])  
or sth. similar.

greets
