neon
neon

Reputation: 2821

On dropdown select, redirect to (Rails 3)

I would like to have a collection of all countries displayed as a dropdown. When a country is selected, I'd like to redirect to the root_path with the selected country as a parameter [i.e. root_path(:country => selected_country)].

I've found the Carmen gem for the list of countries, but can't seem to get it to function properly as specified above!

Upvotes: 0

Views: 846

Answers (2)

Dave Newton
Dave Newton

Reputation: 160231

I probably wouldn't bother with a form post; I'd just set the window.location and construct the URL with the root_path helper and a manually-appended country.

I would, however, validate the country parameter if present.

IMO this approach is a bit inelegant, and I tend to like Derek's suggestion better, but it's an option.

Upvotes: 1

Derek Prior
Derek Prior

Reputation: 3517

Add a change event handler to the select box that posts the form. In your controller action that handles the post, perform the redirect.

http://api.jquery.com/change/

http://api.jquery.com/submit/

Upvotes: 2

Related Questions