title: How to access a hash by key
body: <p>I'm trying to build a Rails helper that will create a nested dropdown menu containing links where the top most is either "All" or the current param and the dropdown contains the other options excluding the current param if there is one.</p>
  
  <p>For example, if I have no <code>post_type</code> param I would see:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;&lt;a href=""&gt;All&lt;/a&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href=""&gt;Discussions&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=""&gt;Snaps&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=""&gt;Code&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=""&gt;Links&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>But if I had a <code>post_type</code> param of 'discussion' then I would see:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;&lt;a href=""&gt;Discussions&lt;/a&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href=""&gt;All&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=""&gt;Snaps&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=""&gt;Code&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=""&gt;Links&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>In my view I have:</p>
  
  <pre><code>&lt;ul class="filter-menu"&gt;
    &lt;li&gt;
      &lt;%= current_post_type(params) %&gt;
      &lt;ul class="filter-menu__drop-down"&gt;
        &lt;%= list_post_types(params) %&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>In my Helper I have:</p>
  
  <pre><code>module PostsHelper
  
    def post_types
      @post_types = {
          :all =&gt; {
              :text =&gt; 'All post types',
              :icon =&gt; 'icon-file-text2',
              :post_type =&gt; nil}
      }, {
          :discussions =&gt; {
              :text =&gt; 'Discussions',
              :icon =&gt; 'icon-bubbles2',
              :post_type =&gt; 'discussions'}
      }, {
          :snaps =&gt; {
              :text =&gt; 'Snaps',
              :icon =&gt; 'icon-images',
              :post_type =&gt; 'snaps'}
      }, {
          :code =&gt; {
              :text =&gt; 'Code',
              :icon =&gt; 'icon-embed2',
              :post_type =&gt; 'code'}
      }, {
          :links =&gt; {
              :text =&gt; 'Links',
              :icon =&gt; 'icon-link',
              :post_type =&gt; 'links'}
      }
    end
  
    def post_type_text(icon, text, drop_down = false)
      raw('&lt;i class="' + icon + '"&gt;&lt;/i&gt; ' + text + (drop_down ? ' &lt;span class="chevron"&gt;&amp;#9662;&lt;/span&gt;' : ''))
    end
  
    def post_type_path(post_type)
      posts_path(:filter =&gt; params[:filter], :time =&gt; params[:time], :post_type =&gt; post_type)
    end
  
    def current_post_type(params)
      if params[:post_type].present? # todo: check matches above
        post_type = params[:post_type].downcase
        link_to post_type_text(post_types[post_type][:icon], post_types[post_type][:text], true), post_type_path(post_types[post_type][:post_type])
      else
        link_to post_type_text(post_types[:all][:icon], post_types[:all][:text], true), post_type_path(post_types[:all][:post_type])
      end
    end
  
    def list_post_types(params)
      post_types.each do |post_type| # todo: exclude current post_type
        link_to post_type_text(post_types[post_type][:icon], post_types[post_type][:text]), post_type_path(post_types[post_type][:post_type])
      end
    end
  
  end
  </code></pre>
  
  <p>How do I access the hash though? I get an error </p>
  
  <pre><code>no implicit conversion of Symbol into Integer
  </code></pre>
  
  <p>when doing <code>post_types[:all]</code>.</p>
  
  <p>I think it's because <code>post_types</code> is returning an array of hashes where as what I want is a hash of hashes accessible by key names.</p>
  
  <p>I could access the <code>:all</code> via <code>post_types[0][:all][:icon]</code> but this won't work for my other hashes, as I want to access them via <code>post_types[post_type][:icon]</code> where <code>post_type</code> is the name of the <code>post_type</code> key I am trying to access.</p>