简体   繁体   English

基于复选框Ruby on Rails的动态数组

[英]Dynamic Array based on check boxes Ruby on Rails

I have two lists of check boxes. 我有两个复选框列表。 One is a list of stores and one is a list of taxonomies that belong to those stores. 一个是商店列表,另一个是属于这些商店的分类法列表。 Right now All taxonomies are shown regardless of what store is selected. 现在,无论选择什么商店,都将显示所有分类法。 How do I display the taxonomies that only belong to the stores that are selected? 如何显示仅属于所选商店的分类法?

html.erb html.erb

<h3>Stores Offered In</h3>
  <ul class="multi-column-checkbox">
    <% for store in Store.all %>
        <li><%= check_box_tag "idea[store_ids][]", store.id, 
@idea.stores.include?(store) %> <%= store.name %></li>
    <% end %>
  </ul>
  <br />


  <h3>Taxonomies Offered In</h3>
      <% for store in Store.all %>
     <% if store.has_taxonomies? %>
          <ul class="multi-column-checkbox-taxonomies" >
            <h4><%= store.name %></h4>
                <% for taxonomy in store.taxonomies 
%>              
                <li><%= check_box_tag "idea[taxonomy_ids][]", 
taxonomy.id, @idea.taxonomies.include?(taxonomy) %> <%= taxonomy.name %></li>
            <% end %>
          </ul>
     <% end %>
  <% end %>

taxonomy.rb 分类学

class Taxonomy < ActiveRecord::Base
  validates_presence_of :name, :store_id
  belongs_to :store
  has_and_belongs_to_many :ideas, :join_table => "taxonomies_ideas"

  validates_uniqueness_of :name, :scope => :store_id
end

store.rb store.rb

class Store < ActiveRecord::Base
  validates_uniqueness_of :code
  has_and_belongs_to_many :ideas, :join_table => "stores_ideas"
  has_many :taxonomies

  def has_taxonomies?
    taxonomies.count > 0
  end
end

I tried to create a helper in my application_helper.rb: 我试图在application_helper.rb中创建一个帮助器:

def show_hide(show)
  show ? 'block' : 'none'
end

and put this in my view: 并在我看来:

 <h3>Taxonomies Offered In</h3>
      <% for store in Store.all %>
     <% if store.has_taxonomies? %>
            <ul class="multi-column-checkbox-taxonomies" style="display: <%= 
      show_hide(@application.______?)%>;" >
            <h4><%= store.name %></h4>
                <% for taxonomy in store.taxonomies 
%>              
                <li><%= check_box_tag "idea[taxonomy_ids][]", 
taxonomy.id, @idea.taxonomies.include?(taxonomy) %> <%= taxonomy.name %></li>
            <% end %>
          </ul>
     <% end %>

but I don't know the name of the method 但我不知道方法的名字

I would like these store check boxes to control what taxonomies are shown possibly through a toggle function. 我希望这些商店复选框可以控制通过切换功能显示的分类法。

You need 2 things: 您需要两件事:

  1. A way to map between the two datasets. 在两个数据集之间进行映射的方法。 Eg a map of which taxonomies belong to which stores. 例如,哪些分类法属于哪些商店的地图。

  2. A way to swap the values in the second set of check boxes based on the first set of check boxes. 一种基于第一组复选框交换第二组复选框中的值的方法。 jQuery is a good candidate, but any javascript or even a page reload will do. jQuery是一个不错的选择,但是任何JavaScript甚至页面重新加载都可以。

There is a similar question here along with some jsfiddles that should help you. 这里有一个类似的问题以及一些应该帮助您的jsfiddles。 They use dropdowns instead of check boxes, but the concepts are the exact same: using jquery how do I filter a dropdown field based on value selected from another dropdown field 他们使用下拉列表而不是复选框,但是概念是完全相同的: 使用jquery,如何根据从另一个下拉列表字段中选择的值过滤下拉列表字段

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

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