Rails自举颜色取决于值

时间:2017-01-24 10:21:40

标签: ruby-on-rails

Project.status可以是[1,2,3]。 有没有更好的方法来定义以下逻辑(颜色取决于status),而不是在视图中进行?

- if project.status == '1'
  %td.bg-success= project.status
- elsif project.status == '2'
  %td.bg-warning= project.status
- else
  %td.bg-danger= project.status

3 个答案:

答案 0 :(得分:1)

您可以通过以下方式实现它。在您看来:

- color_class = {'1': 'success', '2': 'warning'}
- default_class = 'bg_danger'

%td{class: (color_class[project.status] || default_class)}= project.status

我没有 - else意味着什么 - 是其他任何状态还是仅仅是第三种状态。如果是其他任何内容 - 那么default_class是合适的,否则,只需将另一个键值添加到color_class

另请查看ActiveRecord enums状态。

答案 1 :(得分:0)

// Create a list containing all the elements from the HTML list.
IList<IWebElement> listOfElements = driver.FindElements(By.XPath("//ul[@id='ArrangementsWN_BusinessUnitId_listbox']/li"));
// Click on the desired element
listOfElements[1].Click();

答案 2 :(得分:0)

application_helper.rb:

module ApplicationHelper
    def plan_label(plan)
        plan_span_generator(plan)
    end
    private
        def status_span_generator(status)
            case status
                when 'active'
                    content_tag(:span, status.titleize, class: 'badge badge-success')
                when 'inactive'
                    content_tag(:span, status.titleize, class: 'badge badge-secondary')
                when 'planned' 
                    content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-primary')
                when 'confirmed'
                    content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-success')
                when 'member_cancelled'
                    content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-danger')
                when 'client_cancelled'
                    content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-danger')
                when 'no_show'
                    content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-danger')
                when 'no_show_refunded'
                    content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-success')
            end
        end
end

查看: 假设event.status == 'confirmed'。我在视图中放置了= event.status而不是视图中的= status_label(event.status),并且给了该状态类似“徽章徽章成功”的类。  有效!