I'm building a Rails 4 application.
The application serves to document network hardware devices for a bunch of clients (an app an IT consulting business would use). The first index view is one that lists all the clients. At that point, you can click on a link to view items specific to that client. I've built out different models and controllers for the different types of things I'm documenting (i.e. servers controller, firewall controller, etc.).
I've gotten to the point with nesting where I can get the link from the client index page to pass along the client.id
and view items associated with that client on that SPECIFIC controller (i.e. it will list all the servers for that client). Now when you are there I want it to also list all the other items that are from different models/controllers as well. I want one page that displays data from the different models/controllers.
I'm trying to determine what the best practice is for a scenario like this. I feel like I'm missing the obvious and that Rails deals with this in some specific way - or - that most people say Rails sucks/does not do this and everyone does X to accomplish this task.
Here are the ideas I have in no particular order:
client.id
and then write index actions with instance variables for all the other controllers. Already doing this for the nesting and it works, but just for the original request link that contained the client.id
. I can't figure out how to pass the client.id
to the other instance variables so the correct data is displayed. I feel like I could fix this bug but it seems like I'm going down the wrong path.I want to make sure I'm doing this in the good ole fashioned DRY sense.
So what you are describing is something like a dashboard. There's no single "best" way to handle this, but here is what I would do.
Your controller should define the objects that you will be using in your views. Right now I suspect that you're using something like servers_controller.rb
which is only defining @servers
. If your view needs other models, just define them in your action. Let's say you need to list firewalls in addition to servers. Go ahead and add @firewalls = Firewall.where(client_id: client_id)
or something along those lines. Just make sure you don't start adding business logic to your controller.
Once you have your additional variables defined, you can access them in your views. If you want to be DRY about it, you can create a directory for your dashboard partials (assuming you plan on reusing the same views elsewhere). Then you can use <%= render "dashboard/firewalls", firewalls: @firewalls %>
to render that partial and pass it to your @firewall variable.
That's just one idea based on what you provided about your setup.