Suganya
Suganya

Reputation: 741

Filtering a dropdown based on the value in another dropdown in Rails

I have a Country model. In view I have a country dropdown. If country1 is selected, all states of that country must be listed in states dropdown box. If country2 is selected, only 'others' must be displayed in the drop down box. I have jquery to do this. but how do i access a constant defined in ruby in jquery? How do I do that?

          $('#country_id').change(function() {
            debugger
            var country = $('#country_id').val();
             if (country != 'India') {
                // $('#country_state').val("others");
                //$('#country_state').prop("disabled", true);
                $('#country_state').empty().append('<option>Other</option>');
                $('#phone').focus();

            }
            else{
             $('#country_state').empty().append('<option>indiastates*</option>');
            }

        })

*indiastates is a constant in ruby. How do i display that as options for dropdown?

Upvotes: 0

Views: 1213

Answers (2)

Suganya
Suganya

Reputation: 741

For the second dropdown list, I first created an array with the values i wanted to display in the dropdown.

 var states_array  = new Array("xxx","yyy");     

to append this into dropdown

 var states_option;
   for(var i=0;i<states_array.length;i++)
       states_option += "<option>" + states_array[i] + "</option>";
   $school_state.empty().append( states_option );

Upvotes: 0

korada
korada

Reputation: 586

you can try grouped_collection_select

here is the railscast http://railscasts.com/episodes/88-dynamic-select-menus-revised

Upvotes: 1

Related Questions