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

Jeff Carlson
Jeff Carlson
2,178 Points

collection_select and javascript to react to change in selection.

I have been dabbling in rails for 6 months + and just took the rails course here. I have most of the first site done. I am stuck trying to do something a little more UI heavy than anything I have seen.

I have a collection_select control. I use this to select a student. When I select a student I need the student information to change, and then in a separate fragment, I need the Grade and Class combinations to be shown for that student. Grade is what links Student to Class.

The issue is that I can capture the event from the collection_select control with js using $(this).find(":selected").text(); or $(this).find(":selected").val(); How do I: (1) Get the return of the js function back to the controller and (2) call a method on the controller to repaint the page, based on the newly selected Student. (Maybe 2 and 1 are the same thing really).

I have looked all over here for an example, all over Rails Screencasts and I see nothing. I would love it if someone from this community could help and if maybe guys at teamtreehouse.com could build an example of something like this to show me the proper ruby way of doing something like this.

Thanks! Jeff

1 Answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Jeff Carlson this is a great question! Once you get the selected value, you can use that to fire off an ajax request to the controller and update other UI on the page with that data. It's a bit complicated but check out jQuery's .ajax method as well as some of the later lessons on where we use ajax.