View或Controller中的技术显示逻辑?

时间:2016-11-20 19:26:30

标签: ruby-on-rails ruby

我目前正在使用字体集将特定字符交换为符号。

// The .map() array method loops over the original array and creates a new
// one with content that is determined by the function passed to .map()
var htmlList = data['productDetailOptions'].map(item => "<li>" + item + "</li>" );


$('.cart-productDetailOptions ul').append(htmlList );

我觉得这个大量哈希应该存在于控制器方面,但我有问题将其移除。

<% provide(:title, @card.name) %>
<div id="section" class="container-fluid cardLoader">
  <div class="cardContainer">
    <strong class='cardTitle'><%= @card.name %></strong><br>
    <div class="cardImage">
      <%= image_tag(@card.image_url) %>
    </div>
    <div class="cardStats">
      <ul class='cardStatLabel'>
        <li><span class='cardLabel'>Card Name</span>: <%= @card.name %></li>
        <span class='cardLabel'>Mana Cost</span>: 
        <!-- Hash Logic -->
        <% hash = { '{hw}' => '<span class="ms-half">
                            <i class="ms ms-w ms-cost"></i>
                           </span>', 
                '{W}' => '<i class="ms ms-w ms-cost ms-shadow"></i>', 
                '{R}' => '<i class="ms ms-r ms-cost ms-shadow"></i>',
                '{U}' => '<i class="ms ms-u ms-cost ms-shadow"></i>',
                '{G}' => '<i class="ms ms-g ms-cost ms-shadow"></i>',
                '{B}' => '<i class="ms ms-b ms-cost ms-shadow"></i>',
                '{W/U}' => '<i class="ms ms-wu ms-split ms-cost"></i>',
                '{W/B}' => '<i class="ms ms-wb ms-split ms-cost"></i>',
                '{2/W}' => '<i class="ms ms-2w ms-split ms-cost"></i>',
                '{U/B}' => '<i class="ms ms-ub ms-split ms-cost"></i>',
                '{U/R}' => '<i class="ms ms-ur ms-split ms-cost"></i>',
                '{2/U}' => '<i class="ms ms-2u ms-split ms-cost"></i>',
                '{B/R}' => '<i class="ms ms-br ms-split ms-cost"></i>',
                '{B/G}' => '<i class="ms ms-bg ms-split ms-cost"></i>',
                '{2/B}' => '<i class="ms ms-2b ms-split ms-cost"></i>',
                '{R/G}' => '<i class="ms ms-rg ms-split ms-cost"></i>',
                '{R/W}' => '<i class="ms ms-rw ms-split ms-cost"></i>',
                '{2/R}' => '<i class="ms ms-2r ms-split ms-cost"></i>',
                '{G/W}' => '<i class="ms ms-gw ms-split ms-cost"></i>',
                '{G/B}' => '<i class="ms ms-gb ms-split ms-cost"></i>',
                '{2/G}' => '<i class="ms ms-2g ms-split ms-cost"></i>',
                '{0}' => '<i class="ms ms-0 ms-cost ms-shadow"></i>',
                '{1}' => '<i class="ms ms-1 ms-cost ms-shadow"></i>',
                '{2}' => '<i class="ms ms-2 ms-cost ms-shadow"></i>',
                '{3}' => '<i class="ms ms-3 ms-cost ms-shadow"></i>',
                '{4}' => '<i class="ms ms-4 ms-cost ms-shadow"></i>',
                '{5}' => '<i class="ms ms-5 ms-cost ms-shadow"></i>',
                '{6}' => '<i class="ms ms-6 ms-cost ms-shadow"></i>',
                '{7}' => '<i class="ms ms-7 ms-cost ms-shadow"></i>',
                '{8}' => '<i class="ms ms-8 ms-cost ms-shadow"></i>',
                '{9}' => '<i class="ms ms-9 ms-cost ms-shadow"></i>',
                '{10}' => '<i class="ms ms-10 ms-cost ms-shadow"></i>',
                '+2' => '<i class="ms ms-loyalty-up ms-loyalty-2"></i>',
                '−1' => '<i class="ms ms-loyalty-down ms-loyalty-1"></i>',
                '−10' => '<i class="ms ms-loyalty-down ms-loyalty-10"></i>',
                }%>
        <%= @card.manaCost.gsub(/\{.+?\}/){|k| hash[k] || k }.html_safe %></li>
        <li><span class='cardLabel'>Colors</span>: <%= @card.colors %></li>
        <li><span class='cardLabel'>Rarity</span>: <%= @card.rarity %></li>
        <li><span class='cardLabel'>Set</span>: <i class="ss ss-<%= @card.set.downcase %> ss-3x ss-common"></i>  <%= @card.setName %> - <%= @card.set %></li>
        <% if @card.text.nil? %>

        <% else %>
          <li><span class='cardLabel'>Card Text</span>: 
          <%= @card.text.gsub(/\{.+?\}|[\+\d:]/){|k| hash[k] || k }.html_safe %></li>
        <% end %>
        <% if @card.flavor.nil? %>
        <% else %>
          <li><span class='cardLabel'>Flavor Text</span>: <i><%= @card.flavor %></i></li>
        <% end %>
        <% if @card.power.nil? %>
        <% else %>
            <li><span class='cardLabel'>P/T</span>: <%= @card.power %>/<%= @card.toughness %></span></li>
        <% end %>
      </ul>
    </div>
  </div>
</div>

导致att.gsub崩溃。我的哈希&#39;逻辑也可以简化,也许?我想通了

如何将它移动到控制器/它应该存在于控制器中,对吗?

1 个答案:

答案 0 :(得分:0)

正如奥列格在评论中所建议的那样,帮助&#39;是我想要的。

工作助手:

module CardsHelper
  def card_text_mana card
    hash = {    '{hw}' => '<span class="ms-half">
                            <i class="ms ms-w ms-cost"></i>
                           </span>', 
                '{W}' => '<i class="ms ms-w ms-cost ms-shadow"></i>', 
                '{R}' => '<i class="ms ms-r ms-cost ms-shadow"></i>',
                '{U}' => '<i class="ms ms-u ms-cost ms-shadow"></i>',
                '{G}' => '<i class="ms ms-g ms-cost ms-shadow"></i>',
                '{B}' => '<i class="ms ms-b ms-cost ms-shadow"></i>',
                '{W/U}' => '<i class="ms ms-wu ms-split ms-cost"></i>',
                '{W/B}' => '<i class="ms ms-wb ms-split ms-cost"></i>',
                '{2/W}' => '<i class="ms ms-2w ms-split ms-cost"></i>',
                '{U/B}' => '<i class="ms ms-ub ms-split ms-cost"></i>',
                '{U/R}' => '<i class="ms ms-ur ms-split ms-cost"></i>',
                '{2/U}' => '<i class="ms ms-2u ms-split ms-cost"></i>',
                '{B/R}' => '<i class="ms ms-br ms-split ms-cost"></i>',
                '{B/G}' => '<i class="ms ms-bg ms-split ms-cost"></i>',
                '{2/B}' => '<i class="ms ms-2b ms-split ms-cost"></i>',
                '{R/G}' => '<i class="ms ms-rg ms-split ms-cost"></i>',
                '{R/W}' => '<i class="ms ms-rw ms-split ms-cost"></i>',
                '{2/R}' => '<i class="ms ms-2r ms-split ms-cost"></i>',
                '{G/W}' => '<i class="ms ms-gw ms-split ms-cost"></i>',
                '{G/B}' => '<i class="ms ms-gb ms-split ms-cost"></i>',
                '{2/G}' => '<i class="ms ms-2g ms-split ms-cost"></i>',
                '{0}' => '<i class="ms ms-0 ms-cost ms-shadow"></i>',
                '{1}' => '<i class="ms ms-1 ms-cost ms-shadow"></i>',
                '{2}' => '<i class="ms ms-2 ms-cost ms-shadow"></i>',
                '{3}' => '<i class="ms ms-3 ms-cost ms-shadow"></i>',
                '{4}' => '<i class="ms ms-4 ms-cost ms-shadow"></i>',
                '{5}' => '<i class="ms ms-5 ms-cost ms-shadow"></i>',
                '{6}' => '<i class="ms ms-6 ms-cost ms-shadow"></i>',
                '{7}' => '<i class="ms ms-7 ms-cost ms-shadow"></i>',
                '{8}' => '<i class="ms ms-8 ms-cost ms-shadow"></i>',
                '{9}' => '<i class="ms ms-9 ms-cost ms-shadow"></i>',
                '{10}' => '<i class="ms ms-10 ms-cost ms-shadow"></i>',
                '+2' => '<i class="ms ms-loyalty-up ms-loyalty-2"></i>',
                '−1' => '<i class="ms ms-loyalty-down ms-loyalty-1"></i>',
                '−10' => '<i class="ms ms-loyalty-down ms-loyalty-10"></i>',
                }
    card.gsub(/\{.+?\}|[\+\d:]/){|k| hash[k] || k }.html_safe 
  end
end

查看逻辑

<% provide(:title, @card.name) %>
<div id="section" class="container-fluid cardLoader">
  <div class="cardContainer">
    <strong class='cardTitle'><%= @card.name %></strong><br>
    <div class="cardImage">
      <%= image_tag(@card.image_url) %>
    </div>
    <div class="cardStats">
      <ul class='cardStatLabel'>
        <li><span class='cardLabel'>Card Name</span>: <%= @card.name %></li>
        <span class='cardLabel'>Mana Cost</span>: 
        <%= card_text_mana(@card.manaCost) %></li>
        <li><span class='cardLabel'>Converted Mana Cost</span>: <%= @card.cmc %></li>
        <li><span class='cardLabel'>Rarity</span>: <%= @card.rarity %></li>
        <li><span class='cardLabel'>Set</span>: <i class="ss ss-<%= @card.set.downcase %> ss-3x ss-common"></i>  <%= @card.setName %> - <%= @card.set %></li>
        <% if @card.text.nil? %>

        <% else %>
          <li><span class='cardLabel'>Card Text</span>: 
          <%= card_text_mana(@card.text) %></li>
        <% end %>
        <% if @card.flavor.nil? %>
        <% else %>
          <li><span class='cardLabel'>Flavor Text</span>: <i><%= @card.flavor %></i></li>
        <% end %>
        <% if @card.power.nil? %>
        <% else %>
            <li><span class='cardLabel'>P/T</span>: <%= @card.power %>/<%= @card.toughness %></span></li>
        <% end %>
      </ul>
    </div>
  </div>
</div>

现在完美运作。 (可能会重新命名)