# How to dynamically create a controller?

**URL:** <https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891>\
**Category:** Rails\
**Created:** [February 17, 2007, 11:21am UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891 "2007-02-17T11:21:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 11:21am UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/1 "2007-02-17T11:21:30Z")

</div>

I’m making a website for newspaper.  
While implemeting ARCHIVE function, I met a problem.  
I expect that “…/main/archive/volume\_number” is redirected to  
“…/volume\_number/index”.  
However, there are so many volumes in archives and I can’t make all  
controllers manually, each for one volume.  
So, in “main” controller, in “archive” method, I want to dynamically  
create a controller named volume\_number(probably specified by  
params[:id]) and redirect to the controller.  
Please help =)

If you have another idea for implementing archives, please tell me.  
I’m using Mysql and articles are categorized by volume number.

---

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/2 "2007-02-17T15:45:34Z")

</div>

Are these archives in separate databases? I don’t see you needing to  
create  
separate controllers for each archive. You might have to juggle some  
table  
naming features but nothing terribly insane. [I do it for a multi-site app I’m working on where each site has their own content db.] Without  
knowing  
more about the particular constraints you’re under I can’t/won’t hazard  
any  
further speculation but I wouldn’t give up just yet.

RSL

---

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/3 "2007-02-17T17:11:01Z")

</div>

Thank you for your attention.

Archives are not in separate databases.  
All articles are in one database(named ‘entries’), and the database just  
has the attribute named ‘volume’.  
The main page shows the lastest volume, that is newest.  
When someone clicks the ‘archives’ button and choose the volume number,  
then screen changes and shows that volume in the same way as the lastest  
volume is shown.(I don’t mean that each volume has just one page. You  
can think one volume is one group of many pages.)  
Therefore, each volume must be distinguishable but must have the same  
structure on screen.

Thanks.

Russell N. wrote:

> Are these archives in separate databases? I don’t see you needing to  
> create  
> separate controllers for each archive. You might have to juggle some  
> table  
> naming features but nothing terribly insane. [I do it for a multi-site app I’m working on where each site has their own content db.] Without  
> knowing  
> more about the particular constraints you’re under I can’t/won’t hazard  
> any  
> further speculation but I wouldn’t give up just yet.
> 
> RSL

---

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/4 "2007-02-17T17:23:47Z")

</div>

As I see it you’re just wanting to index them by volume which is  
snaptastically easy. First you’ll need to make your routing aware of the  
the  
volume parameter. map.connect “/main/archive/:volume” will give you  
@volume  
[as a String, mind you] to use in your controller. In the controller  
just do  
something like Entry.find(:all, :conditions =\> [“volume = ?”, @volume]).  
You  
might need to change that @volume to @volume.to\_i but might not. This  
isn’t  
the whole solution [since 1) I don’t know the rest of your code and 2)  
you’re the one getting paid for it – hopefully] but should get you  
moving  
in the right direction.

RSL

---

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/5 "2007-02-17T18:05:20Z")

</div>

Thank you very much.  
It’s a very great solution for me.  
Thanks =)

Russell N. wrote:

> As I see it you’re just wanting to index them by volume which is  
> snaptastically easy. First you’ll need to make your routing aware of the  
> the  
> volume parameter. map.connect “/main/archive/:volume” will give you  
> @volume  
> [as a String, mind you] to use in your controller. In the controller  
> just do  
> something like Entry.find(:all, :conditions =\> [“volume = ?”, @volume]).  
> You  
> might need to change that @volume to @volume.to\_i but might not. This  
> isn’t  
> the whole solution [since 1) I don’t know the rest of your code and 2)  
> you’re the one getting paid for it – hopefully] but should get you  
> moving  
> in the right direction.
> 
> RSL

---

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/6 "2007-02-17T18:51:57Z")

</div>

Hi~

On Feb 17, 2007, at 8:23 AM, Russell N. wrote:

> RSL

Actually /main/archive/:volume will not give you @volume, it will  
give you params[:volume]

-Ezra

> then screen changes and shows that volume in the same way as the
> 
> > Are these archives in separate databases? I don’t see you needing to  
> > any
> 
> >

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![zukjimote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/df705f/32.png) [@zukjimote](https://www.ruby-forum.com/u/zukjimote)\
**Post date:** [February 17, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-create-a-controller/85891/7 "2007-02-17T20:24:12Z")

</div>

D’oh! You’re right! Now I’m scratching my head trying to figure out why  
I  
wrote [and _thought_] that when I do know better. Perhaps some brainfart  
about the long-deprecated @params variable? Sure, let’s blame that.  
[Shame  
on me.]

RSL
