jeudi 8 septembre 2016

IF/ELSE Conditions on Ruby hash

I want to set up a Ruby partial that takes a 'type' then, depending on that type, spits out a <li> with defined elements (such as an icon, a particular label, etc)

Here's my working .erb chunk

      <ul>
        <% if locals.has_key? :sermon_links %>
          <% sermon_links.each do |link| %>

            <% if locals.has_key? :type == "download" %>

            <li>
              <i class="material-icons">music_note</i>
              <a href="<%= link[:hyperlink] %>">Download this sermon (~5mb)</a>
            </li>

            <% else %>

            <% if locals.has_key? :type == "passage" %>

            <li>
              <i class="material-icons">link</i>
              <a href="<%= link[:hyperlink] %>"><%= sermon_passage %> on Bible Gateway</a>
            </li>

            <% end %>

          <% end %>
        <% end %>
      </ul>

And then I'd call it in the HTML file like this:

  :sermon_links => [
    { :type => "download", :hyperlink => "http://ift.tt/2ctuCqk" }
  ]

I'm 99% sure the problem is how I'm setting the 'IF type equals THIS' (<% if locals.has_key? :type == "download" %>) I'm a newbie to Ruby on Rails so any help in this area would be really appreciated :) thanks!

Aucun commentaire:

Enregistrer un commentaire