Topic: Rails beginner question: how do i structure controller?

Hi,

Just starting out with RoR and am very eager to move past the basic example apps on all the Web tutorials...

I am unclear though on how to structure something though in Rails:

Let's say I want to create a Web app that allows people to select one of many accounts.  Once an account is selected, they are then taken to a screen where are the associated expenses for that account are listed.  The user can go through the typical add/edit/delete for those expenses (linked to this account via FK).

So let's say, I have two models, Account and Expense.

Should I create two separate controllers, one for each?  If so, how do I keep in context the ID of the Account that I am currently working on (outside of using sessions)?

Is there a way to pass the ID param so that a "subcontroller" is somehow utilized, like maybe:

http://localhost:3000/Accounts/13/Expenses/show/5  ("Show Expense with ID 5 which is tied to Account 13")?

If so, how do I implement this?  If not, how should I be structuring this?

Thanks in advance for everyone's help.

Re: Rails beginner question: how do i structure controller?

okay, googling, more it seems my question is mainly answered by using nested resources/RESTful design.  am I correct in this assumption?

what about the naysayers of REST?  how do these people implement these type of relationships in Rails?

thanks!

Re: Rails beginner question: how do i structure controller?

REST is a great way to go. Even if you don't go with REST, I highly recommend removing multiple scopes in controllers.

schav wrote:

Should I create two separate controllers, one for each?

Yep, that way each controller only manages one scope: the account or expense model.

schav wrote:

If so, how do I keep in context the ID of the Account that I am currently working on (outside of using sessions)?

Nested routes which you mentioned will help. However I don't think they are completely necessary.

You only need the account id in two places: listing the expenses and creating a new expense. You don't need it while destroying or editing an expense because the account_id can be referenced through the expense record itself. In that case nested routes is a little redundant.

As for listing the expenses, you need to decide if this belongs in the "show" action of the Accounts controller or the "index" action of the Expenses controller. If you go with nested routes, it will use the "index" action of the expenses controller.

This is one thing I don't like about nested routes because I feel the page needs to have more about the account itself. You need the name of the account, maybe some other account details along with it. Placing account specific details in the index action of the Expenses controller doesn't feel right to me.

If you list the expenses in the show action of the accounts controller then the only time you need to pass the account id along is while creating an expense. This isn't too hard to do. I can post code if you want.

Railscasts - Free Ruby on Rails Screencasts

Re: Rails beginner question: how do i structure controller?

Thanks for your answer!

If I choose to not implement nested scopes, I can still pass my current Account ID (account_id) as an additional param, right?  So even though I may be working on http://localhost:3000/expenses/show/14 (showing expense #14), I can pass a URL param of account_id with it, correct?

Regardless, I will dig into the RESTful/nested resources tutorials tonight to see if I can get that working.

Thanks!

Re: Rails beginner question: how do i structure controller?

schav wrote:

If I choose to not implement nested scopes, I can still pass my current Account ID (account_id) as an additional param, right?  So even though I may be working on http://localhost:3000/expenses/show/14 (showing expense #14), I can pass a URL param of account_id with it, correct?

Yep, you can pass any parameters you want. It will just be appended to the URL:
http://localhost:3000/expenses/new?account_id=1

Railscasts - Free Ruby on Rails Screencasts