when dissertation editing , I found the answer to your question. soon I'll help you.
Hello and welcome.
Since you want to reuse the query to the database for all users, you'll want to store this in a method. The perfect place for this is in the application controller. The application controller is the controller that dictates various things for all the controllers. Such as the layout to use across the application. What you want to do is setup a protected declaration. This makes sure the method isnt' publically available to the whole rails application, just the application_controller and classes that inherit from it (other controllers that you create via the rails generators).
after you type the line protected, you'll want to create the method to return all the users. like so:
@users = User.order(:name)
Now, you need to call a before_filter. A before_filter calls a method, or executes a block of code that you wish to run before each action call in that controller. Since we're setting this in the application controller, it will apply to all actions.
So at the top, below the line protect_from_forgery, put: before_filter :all_users
Now you should have the @users available to all controllers.
The simplest way to have this list on all pages is to include it on your main layout via a partial. A partial is a small section of a webpage that you build to reuse multiple times.
In the app/views/ folder, create a folder called shared (or whatever you prefer). In there create a new file called _user.html.erb.
In there, you just need one line of code: | |
Change the property names and such as required :).
Now, in your layout file (views/layouts/application.html.erb by default), figure out where you want to have this side bar. And call this method:
'shared/user', :collection => @users %>
Im calling the render method, passing the partial of user. Note how i left out the underscore. I declare the collection to be the @users variable we setup in the before filter. The power of the partial loops through the users, and passes a local variable of each user called user. Hence why i was able to use that in the partial.
In case this is confusing, i put a small test rails app using this on github: https://github.com/agmcleod/WorkingWithRailsListingAllUsers
Thanks in advance for any and all patience you have for me, I'm just getting started teaching myself rails. I'm extremely new to Rails (And relatively new to programming) in general, so excuse any poorly asked questions.
I am experimenting with a very simple sample tracking project for a small analytical lab. I would like to have a side-bar on the site listing all the users in the database with links to view and edit actions. I can figure out the linking part, but I'm having a bit of a hard time figuring out how to display a list of every user in the database.
Any help would be greatly appreciated, even if it's just to a link to another thread that I didn't know how to meaningfully search for. Thanks!
Edit: Here's my rough logical stab at attacking it: 1) Query the database for all names and store in an array or hash. 2) Write a block that takes every element, lists it, and produces links.
I'm just not sure how to make the query. Is my logic sensical?
Edit 2: Got ahead a little bit, but I hit another sticking point.
Ok, I have an index action in my users controller that creates an instance variable called @users containing all users in my database. I then render a template in my sidebar that executes a block that displays all of the users. It only works on the users index page, obviously, since @users creation is defined by the index action. Therefore, my question is either how to I make a variable from user data that's accessible to multiple controllers or how do I just page through the contents of the user database from another controller? That or should I be implementing this in a completely different way?
Conclusion: For some reason I was convinced I couldn't access user model data from the application controller/view. Popped an @users definition into the application controller and it worked like a charm.