# Advice two almost identical controllers

**URL:** <https://www.ruby-forum.com/t/advice-two-almost-identical-controllers/121879>\
**Category:** Rails\
**Created:** [December 2, 2007, 11:07am UTC](https://www.ruby-forum.com/t/advice-two-almost-identical-controllers/121879 "2007-12-02T11:07:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 2, 2007, 11:07am UTC](https://www.ruby-forum.com/t/advice-two-almost-identical-controllers/121879/1 "2007-12-02T11:07:11Z")

</div>

Hi List,

I have a table called invoices where I store all the invoices, the ones  
that I create and the ones that I received.

They have the same fields, the same views, the same data, but I must  
differentiate between them.

Actually I have one controller called invoices, and from a starting  
page, I add a parameter into the url for the kind of invoice I want to  
retrieve.

Now, I have to append this extra parameter in all the urls for  
going/accessing the invoices, like pagination urls, edit/show/update/  
…

Would be easier to create another controll for handling this ?

for example, now I have:

[http://127.0.0.1:3000/invoice/list?kind=2](http://127.0.0.1:3000/invoice/list?kind=2)

[http://127.0.0.1:3000/invoice/list?kind=3](http://127.0.0.1:3000/invoice/list?kind=3)

and I could use:

[http://127.0.0.1:3000/invoice\_me/list](http://127.0.0.1:3000/invoice_me/list)

[http://127.0.0.1:3000/invoice\_to/list](http://127.0.0.1:3000/invoice_to/list)

with this approach, I can forget about append the extra url parameter,  
but I’d have two identical controllers, just the difference about id=2  
or id=3.

I’m thinking how to solve this while I learn Ruby on Rails …

thanks,

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 2, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/advice-two-almost-identical-controllers/121879/2 "2007-12-02T15:28:44Z")

</div>

you could try some routing. add to routes.rb before the last two  
automatically generated routes

map.invoices\_in ‘in/invoices/:action/:id’,  
:controller =\> ‘invoices’, :type =\> ‘in’  
map.invoices\_out ‘out/invoices/:action/:id’,  
:controller =\> ‘invoices’, :type =\> ‘out’

so now if you do

[http://localhost:3000/in/invoices/show/10](http://localhost:3000/in/invoices/show/10)

in your invoices controller:

def show  
if params[:type]==‘in’  
# handle incoming invoices  
else  
# handle outgoing invoices  
end  
end

in your view, you can use the named route

\<%= link\_to ‘incoming invoice #1’,  
invoices\_in\_path( :action=\>‘show’,:id =\> 1) %\>

and it will generate

incoming invoice #1

invoices\_in\_path and invoices\_in\_url methods are automatically created  
for you because of his

map.invoices\_in ‘in/invoices/:action/:id’,…

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 2, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/advice-two-almost-identical-controllers/121879/3 "2007-12-02T16:15:38Z")

</div>

hi rubynuby,

Sounds great, I’m going to try it as soon as possible …

What I don’t know yet if my pagination links will work, I’ll try it !

thanks!

rai

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 2, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/advice-two-almost-identical-controllers/121879/4 "2007-12-02T16:39:02Z")

</div>

hello again,

it’s working perfectly, in all places …

🙂

thanks for the idea and solution and help …

regards,

raimon
