0
votes

I have two controllers, UsersController and TransactionsController and I'm wondering what the best practices is for displaying data from both controllers on one view. Should I create partials under each corresponding views directory and then stitch them together in a separate view or do I create one view under layouts? Am I way off?

EDIT:

In my Views folder:

  • devise

    (devise default views are in here)

  • layouts

    _header.html.erb
    application.html.erb

  • transactions

    _form.html.erb
    index.html.erb
    delete.html.erb
    edit.html.erb
    new.html.erb

  • users

    index.html.erb

I want to show both all users in one table and their transactions in another on the root page.

3
Could you provide an example on how data from one controller is related to the data from the otherDaniel
'User' has many transactions and 'Transaction' belongs to 'User'.Craigfoo

3 Answers

0
votes

If transactions belong to user, you should be able to access transaction data on the user page by default (if models&migrations are set up correctly). If your users index.html.erb will be your root page, that should solve your problem.

0
votes

If you are trying to show this data in an administration page for example then your code should go like this

Controllers:
admin_controller.rb -> with action users_transactions

views/admin
users_transactions.html.erb -> loop on users showing the partial of user

views/transcations
_user_transactions.html.erb

views/users
_user.html.erb -> here you will show how a single user will show including the partial of his transactions.
0
votes

So I guess it all depends on 'what' you want to display and 'how'.

Leave the views/layouts Dir for what it was created, storing layouts. Generally cross-view partials are kept in the views/shared Dir, which stores partials that could be rendered from another directories. in your case form transactions/* & users/*.

Plan wisely before adding a file there... maybe you don't really need it ;)