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.