# Param to url mapping

**URL:** <https://www.ruby-forum.com/t/param-to-url-mapping/88943>\
**Category:** Rails\
**Created:** [March 15, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/param-to-url-mapping/88943 "2007-03-15T19:26:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/param-to-url-mapping/88943/1 "2007-03-15T19:26:09Z")

</div>

I’m currently reading “Agile Web D. with Rails (2nd Edition)”  
and I’ve run into something I don’t quite understand.

In one section of the book they have link in the one of the views that  
looks like this:

\<%= link\_to ‘Show’, :action =\> ‘show’, id: =\> product %\>

This produces a link similar to the following:

[http://www.example.com/product/show/1](http://www.example.com/product/show/1)

I understand that product is the controller, show is the action and 1  
is the product id.

Then in the show action of the product controller you would use  
something like this to access the products id:

product = Product.find(params[:id])

What I don’t understand is how Rails is mapping 1 in the url I gave  
above to params[:id].

What if I had something a little more complicated like categories for  
products. Products might be able to fit into many different categories  
so I have a controller called ProductCategories. I have a list action  
which will list all the products for a given category.

A link like this may be

\<%= link\_to “category.name”, :action =\> ‘list’, :id =\> category %\>

which I think will produce the following URL:

[http://www.example.com/product\_categories/list/1](http://www.example.com/product_categories/list/1)

That’s straight forward. But what if a list of products in a category  
is long and I want to be able to sort the order by say price  
(ascending or decending) or name (ascending or descending).

Could I have links like the following:

\<%= link\_to ‘By Price’, :action =\> ‘list’, id: =\> category, :sort\_by  
=\> ‘price’, :sort\_direction =\> ‘asec’ %\>

That the above link look like this:

[http://www.example.com/product\_category/list/1/price/asec](http://www.example.com/product_category/list/1/price/asec)

and if so how is Rails mapping the URL to params?

I hope that make sense.

Eric Stewart  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/param-to-url-mapping/88943/2 "2007-03-15T20:31:03Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’m currently reading “Agile Web D. with Rails (2nd Edition)”  
> and I’ve run into something I don’t quite understand.
> 
> Eric Stewart  
> removed\_email\_address@domain.invalid

You are asking about Routing in Rails, so you need to read about that to  
get the idea how you can control the URL 🙂

You may want to start here 🙂  
[http://manuals.rubyonrails.com/read/chapter/65](http://manuals.rubyonrails.com/read/chapter/65)

good luck 😃

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 16, 2007, 4:07am UTC](https://www.ruby-forum.com/t/param-to-url-mapping/88943/3 "2007-03-16T04:07:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> [http://www.example.com/product/show/1](http://www.example.com/product/show/1)  
> above to params[:id].  
> which I think will produce the following URL:  
> =\> ‘price’, :sort\_direction =\> ‘asec’ %\>
> 
> That the above link look like this:
> 
> [http://www.example.com/product\_category/list/1/price/asec](http://www.example.com/product_category/list/1/price/asec)
> 
> and if so how is Rails mapping the URL to params?
> 
> I hope that make sense.

The key is to look in routes.rb. There is where the symbols are mapped  
to what will eventually end up in params.

E.g. if you look at the bottom of your routes.rb you should see  
something like:

map.connect ‘:controller/:action/:id’

That “:id” is where the params[:id] is coming from. If you change that  
line to, say:

map.connect ‘:controller/:action/:article\_num’

Then params[:id] will be nil and you’ll need to use params[:article\_num]  
to get that path element from the URL.

You can added whatever “named” path elements that you like in your  
routes and those symbols will be accessible in params. E.g.:

map.connect ‘/:year/:month/:day’, :controller =\> “date”

will give you:

params[:year]  
params[:month]  
params[:day]

If you build a URL with a symbol that isn’t in the route it’ll get  
tacked on as a URL query parameter. So if do something like:

link\_to “query url”, :controller =\> “cont”, :action =\> “act”, :id =\>  
“blah”, :whatever =\> “random”

you’ll get:

cont/act/blah?whatever=random

and the value of whatever will be in params[:whatever]

–  
Michael W.
