0

I would like to know if thereis a plugin or a tutorial at least on how to make a dynamic select form?

I am trying to select the car name using f.select and depending on what carname is selected in the next f.select will be displayed only models that belongs_to the car name. I've tried the Railscasts solution, but it seems that it's for older version of rails or I did something wrong. Thanks.

I use rails 3.0 and the railscasts I tried is http://railscasts.com/episodes/88-dynamic-select-menus

rmagnum2002
  • 11,341
  • 8
  • 49
  • 86
  • Could you add some information to your questions which RailsCast you have tried, and what version of Rails you want to use? 3.1? What is the exact sequence you want to implement: "1. I select a car name from a drop-down list 2. The list of car-models should be filled, so that only the models that fit to the car name may be selected" – mliebelt Oct 08 '11 at 14:03
  • and I have more details about it, I managed to relate all the models and I have the drop down list working fine except the dynamic form, I intend to use the jquery.. but till there I am stuck at using meta_search and dropdownlist to createt an advanced search form. here is the fully described problem http://stackoverflow.com/questions/7696765/meta-search-undefined-method-error – rmagnum2002 Oct 08 '11 at 14:13

1 Answers1

3

There is no automatic way (if I understand correctly - when the user chooses carname in some select box, the options in another select box must change accordingly). I will assume carmodel is a select box but if it's a textbox you can still use the same solution. You basically have 2 options:

  • in both options you must hook into on change event of the select box. you can use e.g. jQuery for this: http://api.jquery.com/change/
  • Option 1 without Ajax: output all the car models in JSON format into the page (like var carModels = {...}), and then in on change event of the carname select box, use javascript/jQuery to add only the appropriate models from carModels into the select box options
  • Option 2 with Ajax: on select of the carname select box, send Ajax request ($.post from jQuery) to the server with the information about selected value in carname. the server should give back the list of appropriate car models in JSON format. after you get this result back, just insert all the values into the select box. For returning this data in json format from server you need to create a post action like get_models. To return the data in json format you can use render :text => @models.all.to_json

The Ajax solution will be easier because you need less Javascript logic.

To see how to add options to select box with jQuery look at this question: What is the best way to add options to a select from an array with jQuery?

Community
  • 1
  • 1
mrbrdo
  • 7,968
  • 4
  • 32
  • 36
  • I've found the jquery dynamic tutorial here http://puneetpandey.com/2011/05/jquery-on-rails3-auto-select/ and I intend to use this one cause none of others methods I've looked gave results – rmagnum2002 Oct 08 '11 at 14:10