简体   繁体   中英

Fetching records based on user selection in rails

I have a dress model with category_id and a subcategory_id attribute, I want to make it so that when a user selects a category type from the collection_select the form gets updated with the subcategories for that category in form of a check box for the user to check the subcategories he needs, I can detect when the selection is changed with javascript through $('#dress_category_id').change and which category type was selected through $('#dress_category_id :selected').text() but I have no idea, and my hours of searching have been futile, how to then update the other form fields with data from the subcategory model.

Here is my form code:

<div id="" class="modal-form no-shadow eighty-margin-top">
  <div class="modal-header">
  </div>
  <div class="form-box">
    <header class="ui-min-header">
      <h3 class="letter_pressed light_header ">New Design</h3>
    </header>
    <div class="form-body  row-fluid" style="margin-top: 20px;">
      <%= form_for( @dress, :html => {class: "form-horizontal", role:"form" }) do |f| %>
          <%= render "error_messages", target: @dress %>

          <div class="form-group" id="avatar">
            <%= f.label :category, class: "col-lg-3 control-label" %>
            <div class="col-lg-9">
              <%= f.collection_select :category, Category.all, :id, :name, {:prompt=> "Select a category"}, {class: "form-control input-lg"} %>
            </div>
          </div>
            </div>
          </div>
Subcategories goes here -->
  <div class="form-group"> <%= f.label :name, class: "col-lg-3 control-label" %> <div class="col-lg-9"> <%= f.text_field :name, placeholder: "Name of your design", class: "form-control" %> </div> </div> <div class="form-group"> <%= f.label :keywords, class: "col-lg-3 control-label" %> <div class="col-lg-9"> <%= f.text_field :keywords, class: "form-control" %> </div> </div> <div class="form-group"> <%= f.label :description, class: "col-lg-3 control-label" %> <div class="col-lg-9"> <%= f.text_field :description, placeholder: "About your design", class: "form-control" %> </div> </div> <div class="form-group"> <div class="col-lg-offset-3 col-lg-9"> <button type="submit" class="btn btn-warning">Continue</button><br/> </div> </div> <% end %> </div> </div> </div> 

You might have a look at jQuery Cascading Dropdown Plugin . Either use it directly, or check out how it's done there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM