Skip to content

Loading…

Updated Formtastic example to work with Formtastic 2 #46

Open
wants to merge 3 commits into from

1 participant

@leonid-shevtsov

Hi,

Formtastic 2 deprecated the enum_input helper described in the readme.

Here's an updated version of the code.

Leonid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 17 additions and 1 deletion.
  1. +17 −1 README.rdoc
View
18 README.rdoc
@@ -502,7 +502,23 @@ Second, do not forget to add the +enum_attr+ macro to the generated model and mi
=== Formtastic integration
-You can display the select input in a Formtastic form with a little monkey patching. First, extend Formtastic with an initializer:
+You can make Formtastic generate a select with the enum values filled in automatically.
+
+For Formtastic 2, define a custom input class:
+
+ require 'formtastic'
+
+ class EnumInput < Formtastic::Inputs::SelectInput
+ def raw_collection
+ @raw_collection ||= collection_from_options || collection_from_enum
+ end
+
+ def collection_from_enum
+ @object.enums(method.to_sym).try(:select_options) || []
+ end
+ end
+
+For Formtastic 1, define a custom helper:
require 'formtastic'
Something went wrong with that request. Please try again.