title: How to get the id and show the next field on the basis of that id in ror?
body: <p>I am using rails4. I want to show the sub-category list based on a category which is being selected in a form. I have done with java-script but I think its not a good solution. Please suggest me what should I do?</p>
  
  <p>I want the user to select a category from this</p>
  
  <pre><code>&lt;div class="field"&gt;
      &lt;%= f.label :Category %&gt;&lt;br&gt;
      &lt;%= f.collection_select :category_id, Category.all, :id, :name, { :prompt =&gt; true, :selected =&gt; @product.category_id } %&gt;
  &lt;/div&gt;  
  </code></pre>
  
  <p>After this next field will get updated based on the user selection. Currently I have made hidden these #women_category &amp; #men_category</p>
  
  <pre><code>&lt;div class="field" id="women_category"&gt;
    &lt;%= f.label :sub_category %&gt;&lt;br&gt;
    &lt;%= f.collection_select  :sub_category_id, @first_category.sub_categories.all,:id ,:name%&gt;
  &lt;/div&gt;
  &lt;div class="field" id="men_category"&gt;
    &lt;%= f.label :sub_category %&gt;&lt;br&gt;
    &lt;%= f.collection_select  :sub_category_id, @second_category.sub_categories.all,:id ,:name%&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Now when user selects category_id=1, I display the #women_category and if user selects category_id =2, #men_category is displayed with the help of javascript</p>
  
  <pre><code>$("select[name='product[category_id]']").change(function(){  
    if(this.value == '1'){
      $("#women_category").show();
      $("#men_category").hide();
    }else if(this.value == '2'){
      $("#women_category").hide();
      $("#men_category").show();
    }
  });
  </code></pre>
  
  <p>If my categories list increases it will be complicated, So I can I code this without this much complications? Please help.</p>