Rails Helper If语句

时间:2018-06-13 15:52:15

标签: ruby-on-rails

我正在尝试编写一个帮助程序,它将读取一个整数并设置与它一起使用的字符串以及类。我以为会是这样的......

def credit_status(customer)
if customer.stop_flag == 0
  <span class="badge badge-success">Active</span>

elsif customer.stop_flag == 1
  <span class="badge badge-warning">Closed</span>

elsif customer.stop_flag == 2
  <span class="badge badge-warning">Hard Stop</span>

elsif customer.stop_flag == 3
  <span class="badge badge-danger">Closed</span>
end
end

但是我收到了这个错误

/home/ec2-user/environment/rialto-crm/app/helpers/application_helper.rb:55: syntax error, unexpected '<' <span class="badge badge-danger ^ /home/ec2-user/environment/rialto-crm/app/helpers/application_helper.rb:55: unterminated regexp meets end of file /home/ec2-user/environment/rialto-crm/app/helpers/application_helper.rb:55: syntax error, unexpected end-of-input, expecting keyword_end

我做错了什么?

由于

克里斯

1 个答案:

答案 0 :(得分:0)

我想我会想做一些事情:

STOP_FLAG_MAPPINGS = {
  '0' => {
    class: 'success',
    label: 'Active'
  },
  '1' => {
    class: 'warning',
    label: 'Closed'
  },
  '2' => {
    class: 'warning',
    label: 'Hard Stop'
  },
  '3' => {
    class: 'danger',
    label: 'Closed'
  }
}

def credit_status(customer)
  content_tag(
    :span, 
    STOP_FLAG_MAPPINGS[customer.stop_flag.to_s][:label],
    class: "badge badge-#{STOP_FLAG_MAPPINGS[customer.stop_flag.to_s][:class]}"
  )
end