# Routing Question

**URL:** <https://www.ruby-forum.com/t/routing-question/129848>\
**Category:** Rails\
**Created:** [February 22, 2008, 1:45am UTC](https://www.ruby-forum.com/t/routing-question/129848 "2008-02-22T01:45:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 22, 2008, 1:45am UTC](https://www.ruby-forum.com/t/routing-question/129848/1 "2008-02-22T01:45:41Z")

</div>

So I have a routing problem I’m running into.

I have a model called Film, and each Film belongs to categories, which  
are being store in the DB as a serialized array in the category column.

The films\_controller is working right now with index, new, edit, delete  
actions. All is well using the default rails routing scheme.

What I want to do is keep the films\_controller as close to as it is now,  
but allow the user to view films by category using a url like this

/admin/films/:category/:action/:id

without loosing the ability to manage all films using urls like this:

/admin/films/:action/:id

I’ve tried setting up a route like this, both before and after the  
default rails routes:

map.admin\_films ‘admin/films/:category/:action/:id’, :controller =\>  
‘admin/films’

When I place it before the default rails routes I get the category pages  
working, but the global actions (/admin/films/edit/1) error out with the  
following error:

‘No action responded to 1’

When I place it after the default rails routes the category pages don’t  
work and I get the following error:

‘No action responded to narrative\_features’

(narrative\_features being on of the categories films can belong to)

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 22, 2008, 2:59am UTC](https://www.ruby-forum.com/t/routing-question/129848/2 "2008-02-22T02:59:36Z")

</div>

What you first want to do is to create a separate categories model and  
probably have an habtm relationship between films and categories (a film  
can have many categories and a a category can have many films). You  
should (generally) not do things like serialized arrays in database  
columns for things that are logical entities in your model. Basically,  
this is not a “normalized” database.

For example, if you delete the last film that was part of the category  
“Western”, you would no longer have “Western” as a category.

Paul

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 22, 2008, 3:06am UTC](https://www.ruby-forum.com/t/routing-question/129848/3 "2008-02-22T03:06:03Z")

</div>

Oh, and as far as the issue regarding routes, you might need to use  
regular expressions to help defined “Pretty URLs”

[http://api.rubyonrails.org/classes/ActionController/Routing.html](http://api.rubyonrails.org/classes/ActionController/Routing.html)

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 22, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/routing-question/129848/4 "2008-02-22T19:36:26Z")

</div>

Paul H. wrote:

> What you first want to do is to create a separate categories model and  
> probably have an habtm relationship between films and categories (a film  
> can have many categories and a a category can have many films). You  
> should (generally) not do things like serialized arrays in database  
> columns for things that are logical entities in your model. Basically,  
> this is not a “normalized” database.
> 
> For example, if you delete the last film that was part of the category  
> “Western”, you would no longer have “Western” as a category.
> 
> Paul

I tottally hear ya. I did this knowing it was lazy development, and that  
I would have to rewrite it eventually.

In regards to the routes. Will I use a regex that checks to make sure  
that the :category matches only the available categories? Not sure how  
that is going to solve my problem.

Thanks.
