Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Javascript</strong></p> <pre><code>function update_versions_div(project_id) { jQuery.ajax({ url: "/update_versions", type: "GET", data: {"project_id" : project_id}, dataType: "html" success: function(data) { jQuery("#versionsDiv").html(data); } }); } </code></pre> <p><strong>Controller</strong></p> <pre><code>def edit @projects = Project.all @versions = Version.all end def update_versions @versions = Version.where(project_id =&gt; params[:project_id]).all render :partial =&gt; "versions", :object =&gt; @versions end </code></pre> <p><strong>View</strong></p> <pre><code>&lt;%= select_tag "project_id", options_from_collection_for_select(@projects, "id", "title"), :prompt =&gt; "Select a project", :onchange =&gt; "update_versions_div(this.value)" %&gt; &lt;div id="versionsDiv"&gt; &lt;%= render :partial =&gt; 'versions', :object =&gt; @versions %&gt; &lt;/div&gt; </code></pre> <p><strong>Partial</strong>: _version.html.erb</p> <pre><code>&lt;%= select_tag "version_id", options_from_collection_for_select(versions, "id", "title"), :prompt =&gt; "Select a version" %&gt; </code></pre> <p>Also add a route for <code>/update_versions</code> in your routes.rb</p> <pre><code>match "/update_versions" =&gt; "&lt;controller&gt;#update_versions" </code></pre> <p>Here, you should replace <code>&lt;controller&gt;</code> with name of the controller.</p> <p>I havent tested the code, so there may be errors.</p> <p><strong>Update</strong></p> <p>PullMonkey has updated the code with Rails 3 example, which is obviously superior than this code. Please checkout <a href="http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/" rel="noreferrer">http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/</a> also</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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