title: How to implement this dynamic select options in view using "select_tag"?
body: <p>I have two model objects which are <code>Event</code> and <code>Task</code> , they have <strong>one-to-many</strong> association.</p>
  
  <p>Event model:</p>
  
  <pre><code>class Event &lt; ActiveRecord
    has_many :Task
  </code></pre>
  
  <p>Task model:</p>
  
  <pre><code>class Task &lt; ActiveRecord
    belongs_to :Event
  </code></pre>
  
  <p>My controller has a index function which return the list of events (@events), <strong>Maybe</strong> return also a list of tasks(@tasks):</p>
  
  <pre><code>class MyController &lt; ApplicationController
  
    def index
      @events = Event.all
      @tasks.....
  
    end
  </code></pre>
  
  <p>In my view (index.html.haml), I have defined a multi-select field for all <strong>events</strong> in a table column as below:</p>
  
  <pre><code>%td
    =select_tag('events',options_from_collection_for_select(@events, 'id', 'name'), {:multiple=&gt;'multiple'})
  </code></pre>
  
  <p>the output code of the above select_tag is:</p>
  
  <pre><code>&lt;td&gt;
  &lt;select id="events" multiple="multiple" name="events"&gt;
      &lt;option value="1"&gt;Event 1&lt;/option&gt;
      &lt;option value="2"&gt;Event 2 &lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>What I want is in another table column I have another multi-select field for <strong>tasks</strong> which looks just like the <strong>events</strong> multi-select field, <strong>BUT the key feature is that</strong>, the content of the <strong>tasks</strong> select options is dynamically changed based on which <strong>event or events</strong> user selected from the <strong>events</strong> multi-select field.</p>
  
  <p>For example, suppose event_1 is associated with task_1 and task_2, if user mouse click on event_1, the tasks multi-select field will show task_1 and task_2 as options which is available for user to select next.</p>
  
  <p>How to implement this feature in rails? I am using Rails 3. I guess this feature needs some ajax magic to play, but I have no idea how to implement it, it is better if someone can give a simple example on this issue. Thanks!</p>