Jo Erlang
Jo Erlang

Reputation: 327

Creating a custom input select in formtastic

I want to create a custom input for formtasic that uses a custom collection

My collection is returned by MyModule::Categories.all

I'm extending SelectInput

class CategoriesSelectInput < Formtastic::Inputs::SelectInput
  def select_html
    collection = MyModule::Categories.all
    builder.select(input_name, collection, input_options, input_html_options)
  end

end

but the formatting seems to get lost, where am I going wrong?

Upvotes: 1

Views: 895

Answers (2)

Yuri Tytarenko
Yuri Tytarenko

Reputation: 21

It's a bit late.. but try this way:

class CategoriesSelectInput < Formtastic::Inputs::SelectInput
  def to_html
    collection = MyModule::Categories.all

    builder.input input_name, as: :select, collection: collection, input_html: input_html_options

  end
end

Upvotes: 2

San
San

Reputation: 1954

There is no need to extend select input. Formatstic has awesome support for providing custom collections. Read it's documentation for details: https://github.com/justinfrench/formtastic

In short, you can do something like this:

<%= f.input :some_id, :as => :select, :collection => MyModule::Categories.all %>

Upvotes: -1

Related Questions