2016-12-01 3 views
-1

는 I는 다음과 같다하는 (_card_brand.html.erb)을 다음과 같은 부분을 가지고전달되는 다른 변수의 값에 따라 특정 문자열을 내 partial에 전달하는 방법은 무엇입니까?

<%= render partial: "subscriptions/card_brand", locals: { brand: current_user.card_brand } %> 
: 위이 함께 렌더링

<div class="payment-card"> 
    <i class="fa fa-cc-visa payment-icon-big text-success"></i> 
</div> 

:이 보이는 HTML 렌더링

<div class="payment-card"> 
    <i class="fa fa-cc-<%= brand.downcase %> payment-icon-big text-success"></i> 
</div> 

내가하고 싶은 일은 text-success 등급을 다음 중 하나로 변경하는 것입니다 : 카드가인지 여부에 따라 다릅니다.

  • 비자 = 성공
  • AMEX = 경고
  • 마스터 = 차
  • 발견 = 위험

다른 카드는 다른 것 : 그래서 37,

수업.

내 뷰에서 부분을 렌더링하는 것을 어떻게 우아하게 표현합니까?

답변

1

도우미를 만들어 새 클래스를 쉽게 추가 할 수 있습니다.

application_helper.rb

CARD_CLASS = { 
    'visa' => 'success', 
    'amex' => 'warning', 
    'mastercard' => 'primary', 
    'discovery' => 'danger' 
} 

def payment_class(type) 
    CARD_CLASS[type.downcase] 
end 

_card_brand.html.erb

<div class="payment-card"> 
    <i class="fa fa-cc-<%= brand.downcase %> payment-icon-big text-text-<%= payment_class(brand.downcase) %>"></i> 
</div> 
+0

난 당신이'텍스트를 의미 가정 - <% = payment_class (brand.downcase) %>' . – marcamillion

+0

나는 그것을 고쳤으며 이것은 매력처럼 작동한다. 감사! – marcamillion

0
<i class="fa fa-cc-<%= brand.downcase %> payment-icon-big 
<% if brand == 'Visa' %> 
text-success 
<% elsif brand == 'AMEX' %> 
text-warning 
<% elsif brand == 'Mastercard' %> 
text-primary 
<% elsif brand == 'Discovery' %> 
text-danger 
<%end%> 
"> 
</i>