EastsideDev
EastsideDev

Reputation: 6639

Passing a variable to a Rails SLIM view

Rails 5
SLIM
Bootstrap 4

In one of my views, I have something like this:

- if sales > 2000
  button.btn-primary type="button"
- if sales.between?(1000, 2000)
  button.btn-warning type="button"
- if sales < 1000
  button.btn-danger type="button"

What I would like to do, is resolve this in the Helper, and pass a variable to the view, as the type of button to use. Is this possible?

Upvotes: 1

Views: 270

Answers (1)

Emu
Emu

Reputation: 5905

You can do something like that:

In HELPER:

def sales_button(sales)
    case
    when sales > 2000
      "btn-primary"
    when sales.between?(1000, 2000)
      "btn-warning"
    when sales < 1000
      "btn-danger"
    end
end

In VIEW:

button *{class: sales_button(sales) } type="button"

Upvotes: 2

Related Questions