1

I have been trying for weeks to find a solution for populating a state drop down menu dependent on the selected country. It seems like I will have to use some type of scripting to get this working. Unfortunately I do not understand the modern scripting at all. I have looked at jQuery but could not tell you what it is doing.

I have a User model where a user belongs to a country and state. A Country has many users and many states. A State belongs to a country and has many users.

I subscribed to RailsCasts in order to access the revised Dynamic Select Menu #88 video.

http://railscasts.com/episodes/88-dynamic-select-menus-revised

It looks like the revision is not up to newer versions of Rails. When I attempted to use the code in that RailsCast I had to modify the Rails code slightly. I used the script as it was but changed person to user to match my Rails app. The script is supposed to hide the state info on the screen if there are no states for the selected country. At one point it hid all the fields on the screen, not just the state. Otherwise I just got the entire list of states grouped by country. I decided to remove the script after much aggravation. I'm scared of having my entire edit/create page disappear again. It took me hours and restoring an older version of my app to figure out that the script was causing my problems.

I decided to look at this one since the Ruby on Rails code did not look all that intimidating. However I do not understand where some of this code should go. I also do not know if there is more I would need to do to get this working. Category would be my country and sub-category would be my state. Widget would be my User. At least this is how I am looking at this.

http://samuelmullen.com/2011/02/dynamic-dropdowns-with-rails-jquery-and-ajax/

In the jQuery section I do not know if the 2nd & 3rd pieces of code into application.js or somewhere else. The way it is written I would assume so. In the View section it says to create a view under the categories folder which in my case would be the country folder. I only have a user folder in my view folder. I have no idea where I would put this view at this point.

I have my collection_select statements in a partial which is rendered in new and edit. I do not know if that could cause some confusion. From what little I understand when I look at the source of the page I do not see any difference in the id, label, etc. that I see in either page (user_country_id, user_state_id).

If it is possible I would like to do this using a where clause added to the collection_select statement for the states. That would make it a lot easier for me if it is possible to reference the selected value in the country collection_select statement. I'm thinking something like this:

<%= collection_select(:user, :state_id, State.where(:country_id => user_selected_country_id).order('name'), :id, :name) %>

To say I am completely lost is a huge understatement.

Any help would be appreciated.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364

1 Answers1

0

I'm not too familiar with js/jQuery myself, but these similar questions might help: Observe_field in rails 3 and Rails observe_field using jQuery. The first question mentions this blog that has the code for the jQuery equivalent for the old Prototype observe_field helper method in rails and the second one links to something similar on git.

Community
  • 1
  • 1
  • Since I wrote this question I decided to work on other features of this project. Along the way I read something about observe_field. I will definitely revisit this. I ended up taking a Ruby class last semester. I will be taking a JavaScript online course since my teacher felt that all web developers need this to plug the holes that all web development tools inevitably have. Thanks so much for the suggestion. – Pamela Cook - LightBe Corp Jan 12 '13 at 18:28
  • No prob. I need to learn JavaScript too. In the meantime, you could work around it by using [`grouped_collection_select`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_collection_select) by putting it into one select box instead of two. Let me know what you end of doing with js though, cause I'd be interested. – undefinedvariable Jan 14 '13 at 18:44
  • 1
    I will post it as an answer to this question:) – Pamela Cook - LightBe Corp Jan 16 '13 at 13:31