【问题标题】:Dynamic Array based on check boxes Ruby on Rails基于复选框 Ruby on Rails 的动态数组
【发布时间】:2013-10-09 10:28:26
【问题描述】:

我有两个复选框列表。一个是商店列表,一个是属于这些商店的分类法列表。现在无论选择哪个商店,都会显示所有分类。如何显示仅属于所选商店的分类法?

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

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

我尝试在我的 application_helper.rb 中创建一个助手:

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

在我看来:

 <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 %>

但我不知道方法的名称

我希望这些商店复选框可以通过切换功能来控制可能显示的分类。

【问题讨论】:

    标签: javascript ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    你需要两件事:

    1. 一种在两个数据集之间进行映射的方法。例如。哪些分类属于哪些商店的地图。

    2. 一种基于第一组复选框交换第二组复选框中的值的方法。 jQuery 是一个很好的候选者,但任何 javascript 甚至页面重新加载都可以。

    这里有一个类似的问题以及一些应该对您有所帮助的 jsfiddles。他们使用下拉菜单而不是复选框,但概念完全相同: using jquery how do I filter a dropdown field based on value selected from another dropdown field

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多