Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like the answer you're looking for is a custom form builder. You could create your form builder and inherit from the Rails form builder, and then set that form builder as the default across your application. Then you could define an element called dependent_dropdown or cascading_selection, etc. This element would probably take the id of the source dropdown. Your helper would output a collection_select but it would also output some JavaScript that would fire an AJAX call when the source dropdown changes.</p> <p>Of course, you don't have to do it this way. You could just use a collection_select, add some attributes to the source dropdown (i.e. <code>:class =&gt; 'source_for_dependent', :dependent =&gt; some_id</code>), and then hook up some JavaScript in your application.js that looks for collections with the <code>source_for_dependent</code> class, and when the <code>onchange</code> event fires it grabs the id from the <code>dependent</code> attribute and fires an AJAX call. </p> <p>Either way, here's an example of your JavaScript (using jQuery)</p> <pre><code>$('select.source_for_dependent').change(function() { var id = // get the id of the dependent dropdown, perhaps by $(this).attr('dependent') var dependentDropDown = $('#' + id); dependentDropDown.empty(); var success = function(response) { for (var item in response) { var option = $(document.createElement('option')).val(item.val).html(item.text); dependentDropDown.append(option); } } $.get('/some_controller/some_action/' + $(this).val(), success); } </code></pre> <p>The success handler gets passed into jQuery's <code>get</code> method. It takes a JSON response as an argument. We loop through the response, and for each item, we create an <code>option</code>, pulling the value and the text off the item, and we append that to the dependent dropdown. Your controller might look something like this:</p> <pre><code>def some_action @obj = SomeClass.find(params[:id]) respond_to do |format| format.js { render :json =&gt; @obj } end end </code></pre> <p><strong>Edit</strong></p> <p>Which controller you target is up to you. Let's say Dropdown A targets resource A, and Dropdown B targets resource B. An object of type <code>A</code> should have a list of objects of type <code>B</code>. If you're going after the <code>show</code> action for object <code>A</code>, then your <code>as_json</code> method for object <code>A</code> needs to include its <code>B</code> associations. <a href="http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/" rel="nofollow">This article</a> shows examples of this.</p> <p>Otherwise, you could target the <code>index</code> action for resource <code>B</code>. Making <code>B</code> a nested resource of <code>A</code> would be an easy way to key off the id of <code>A</code> to get all objects of type <code>B</code> which have a foreign key that points to <code>A</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload