Search code examples
ruby-on-railshamlsimple-form

Rails / Simple Form - How to create a select box with HAML?


What's the best way to add a select box with options and default value using Simple Form and HAML?

This is my _form.html.haml content and f.option not working.

= simple_form_for @post do |f|                                                                                                                                    
    = f.input :title       
    = f.input :body
    = f.label :category_id 
    = f.select :category_id, Category.all.map{|category| [category.name, category.id]}
    = f.option{value: "undefined"} 
    %br

= f.button :submit

Any help would be appreciated :)


Solution

  •   def get_select_data
        data = Category.all.map{ |category| [category.name, category.id] }
        data.push([.....])
      end
    

    Probably put this in a service model. Initialize the model in your controller, and use it in your view:

    = f.select :category_id, @service_model.get_select_data
    

    Perhaps this will be an ok start for you