简体   繁体   English

根据用户在Rails中的选择获取记录

[英]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. 我有一个带有category_id和subcategory_id属性的着装模型,我想制作一个衣服模型,以便当用户从collection_select中选择一个类别类型时,该表单将以复选框的形式更新为该类别的子类别,以供用户检查他需要的子类别,我可以通过$('#dress_category_id')。change来检测何时使用javascript更改了选择,以及通过$('#dress_category_id:selected')。text()选择了哪种类别类型,但我不知道,而我的搜索时间却徒劳无功,如何使用子类别模型中的数据更新其他表单字段。

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 . 您可能会看一下jQuery Cascading Dropdown Plugin Either use it directly, or check out how it's done there. 您可以直接使用它,也可以查看其效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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