Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Zachary Pearson
Zachary Pearson
9,578 Points

Rails apps: Displaying items based on user input

Hi all,

This is a fairly vague question - not looking for a concrete answer as much as a push in the right direction. I've been googling around and haven't been able to get closer to what I'm looking for.

I'm working on a rails app that has a main page that displays a collection of documents in a table. Each document has several parameters: title, subject, author, those sorts of things. By default, the table shows the documents in reverse chronological order by time of creation.

What I'd like to have is a collection of drop down menus up above that allow users to sort documents by one (or many) of these fields. So, for example, selecting a certain author and a certain subject would cause the table to only display documents that match both those parameters. Ideally, I'd want to accomplish this without loading a new page.

I know this is very non-specific, but anyone have any leads?

2 Answers

Tim Knight
Tim Knight
28,888 Points

Hey Zachary, I think you're solution here might be doing something with JavaScript and AJAX. If for example you were using jQuery you could write something that would check onchange of those selects that would pass a query parameter to a $.get request, get the data back and load it in a div that contains your results. It's not necessarily a Rails or Ruby specific issue.

That being said, there might be a jQuery or Rails plugin for chaining your select options together too if you want the options in the select fields to change based on the other selections. Here is a jQuery plugin that might help if you're wanting to do something like specifically. http://www.appelsiini.net/projects/chained

I hope that gives you some direction.

Zachary Pearson
Zachary Pearson
9,578 Points

Thanks - that actually helps a lot! Like I said, I wasn't even quite sure where to start looking. I'll start looking into a JavaScript solution and follow up on those forums if I have any trouble.