Search code examples
ruby-on-railsslim-lang

How to populate array in Rails view using Slim syntax


I'm using slim with a small array to populate a select tag.

first attempt:

=f.select (:productline) do
    -[["productlinetest","value"],["[B] Bolted Bonnet"] ].each do |c|
        = content_tag(:option, c.first, value: c.last)

This works fine but if i want to add more options I would have to have them all on the same line. I would prefer something like this:

=f.select (:productline) do
    -[
        ["productlinetest","value"],
        ["[B] Bolted Bonnet"]
        ].each do |c|
        = content_tag(:option, c.first, value: c.last)

but that doesn't work.


Solution

  • Move your array into the controller action, and use @variable in views:

    def my_awesome_action
       @array = [["productlinetest","value"],
                 ["[B] Bolted Bonnet"]]
    end
    

    in views:

    = f.select (:productline) do
        - @array.each do |c|
            = content_tag(:option, c.first, value: c.last)
    

    Slim template engine doesn't allow multi line expression like in Ruby