开发者

Formtastic, own :as input type

How I can add my own field types to formtastic ?

For exemple, I need to have a custom datetime input, and I want something like this:

<%= f.input :start_date 开发者_运维百科, :as => :my_date %>

This obviously doesn't work because formtastic doesn't know the :my_date (only :boolean, :string, :datetime and so on...)

But how can I add additional input types ?


You need to add a custom input method:

class MyCustomFormtasticFormBuilder < Formtastic::SemanticFormBuilder
  protected
  def my_date_input(method, options)
    basic_input_helper(:text_field, :my_date, method, options)
  end
end

That's perfect for, say the new HTML5 input types. You use it like so:

<% form_form @model, :builder => MyCustomFormtasticFormBuilder  do |f| %>
   <%= f.input :start_date, :as => :my_date
<% end %>


Don’t subclass Formtastic::FormBuilder anymore

It was previously recommended in Formtastic 1.x to subclass Formtastic::FormBuilder to add your own inputs. This is no longer recommended in Formtastic 2, and will not work as expected.

https://github.com/justinfrench/formtastic

http://justinfrench.com/notebook/formtastic-2-preview-custom-inputs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜