# Is having controller within controller bad practice?

**URL:** <https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668>\
**Category:** Rails\
**Created:** [February 6, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668 "2007-02-06T21:04:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/1 "2007-02-06T21:04:20Z")

</div>

id like to have my site organized like this…

[nj.com/southern/restaurants/italian/3](http://nj.com/southern/restaurants/italian/3)

to have a url like that, it seems i would need a controller called  
southern with an internal controller restaurants where 3 is the id of  
the item.

thoughts? suggestions? thanks.

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/2 "2007-02-06T21:21:08Z")

</div>

That’s a perfectly valid URL, but it won’t mean  
‘controller-in-controller’.  
Rails routes are incredibly powerful, try something like this (near the  
bottom to allow for other custom routes to take precendence).

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

Then your :controller/:action will get:

params = {  
:region =\> ‘southern’,  
:id =\> 3  
}

Jason

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/3 "2007-02-06T21:55:11Z")

</div>

On 2/6/07, koloa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the item.
> 
> thoughts? suggestions? thanks.

I’ve been trying to make my URLs even more search engine friendly  
lately. I’ve gotten away from using integer values in the URL for  
anything except the admin/non-public side of my applications. I’ve  
been using a :base\_name column combined that with some routing.

In the model I do:

before\_save :base\_name\_from\_title

def base\_name\_from\_title  
self.base\_name = title.downcase.gsub( /\ and\ /, ‘-’ ).  
gsub( /\ on\ /, ‘-’ ).  
gsub( /[^a-zA-Z0-9-]/, ‘-’ ).  
gsub( /[-]+/, ‘-’ ).  
gsub( /[-]$/, ‘’ ).  
gsub( /^[-]/, ‘’ )  
end

“title” is whatever important wordy field I can pick out of a given  
model. It’s tough to pick sometimes especially if the field isn’t  
unique. I don’t have too many collisions on my personal blog though.

Then I add a route:

map.connect “category/:base\_name”,  
:controller =\> ‘category’,  
:action =\> ‘index’,  
:requirements =\> { :base\_name =\> /[\w-]+/ },  
:base\_name =\> nil

Then in the controller I look it up by base\_name instead of by id:

Category.find( :first, :conditions =\> [ ‘base\_name = ?’,  
params[:base\_name] ] )

Don’t forget to index that text field! 🙂

Then my URLs look like:

[http://destiney.com/category/programming-languages-ruby](http://destiney.com/category/programming-languages-ruby)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/4 "2007-02-06T22:34:37Z")

</div>

hi Jason and Greg, Thanks for the post…question though…

lets say my category field in a model can be ‘bbq’, ‘chinese’,  
‘italian’…and id like to have the url

[localhost.com/nj/bbq](http://localhost.com/nj/bbq)

or

[localhost.com/nj/southern/bbq](http://localhost.com/nj/southern/bbq)

where nj is a controller, southern is a :region field and bbq is a  
:mcategory field,both are in my restaurant table…

do i define like this?

map.connect ‘:controller/:region/:mcategory’, :action =\> ‘view’

where view does the logic to determine what to pass in my find statement  
like

def view  
NJ.find(:all, :conditions =\> "mcategory = ? and region =  
?"params[:mcategory, params [:region]])  
end

the name ‘view’, will that be part of the url? i would prefer to have it  
like [localhost.com/nj/southern/bbq](http://localhost.com/nj/southern/bbq)…

thanks.

Greg, id really like the way you did your titles.

Jason R. wrote:

> That’s a perfectly valid URL, but it won’t mean  
> ‘controller-in-controller’.  
> Rails routes are incredibly powerful, try something like this (near the  
> bottom to allow for other custom routes to take precendence).
> 
> map.connect ‘:region/:controller/:action/:id’
> 
> Then your :controller/:action will get:
> 
> params = {  
> :region =\> ‘southern’,  
> :id =\> 3  
> }
> 
> Jason

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/5 "2007-02-06T22:43:49Z")

</div>

whoa…i just tried it and it works and I can have the url that i like!

wow that feature is really awesome… i was about to brute force like so  
many controllers within each other.

now i have to rethink my naming conventions of functions. my brain  
hurts… thanks all for helping a noob.

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/6 "2007-02-06T23:06:04Z")

</div>

another quick question…

i used to have a long case stament like this…

switch on 'food ’ category  
case ‘bbq’  
…  
if state == nj  
do find all in nj for bbq  
if state == pa  
do find etc…  
case ‘chinese’  
…  
if…  
…  
case …  
end

switch on ‘something else’ category  
…  
…

now because of the routes.rb power, i no longer need my case statement  
since i can have…

map.connect ‘:state, :main\_category, :sub\_category’

so when presses a link on my site, it can go to a action that just has  
this?

def getView  
@myObj = params;[:main\_category].capitalize.find(:all, :conditions  
[‘state = ? and subcategory = ?’, params[:state],  
params[:sub\_category]])

end

Thansk!

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/7 "2007-02-06T23:30:59Z")

</div>

Hey Jason, Thanks for explaining this to me! i think i am now going to  
rewrite my app again for the third time cutting the code i have maybe by  
30%!

Jason R. wrote:

> Yep, it would work pretty much like that. Anything on the URL gets split  
> up  
> according to route definitions and placed into params.
> 
> Jason

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [February 6, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/is-having-controller-within-controller-bad-practice/84668/8 "2007-02-06T23:28:24Z")

</div>

Yep, it would work pretty much like that. Anything on the URL gets split  
up  
according to route definitions and placed into params.

Jason
