Search code examples
jqueryruby-on-railsformtastic

Ruby on Rails + Formtastic + jQuery UI datepicker


I'm trying to install the jQuery UI datepicker with Formtastic. I followed http://blog.brzezinka.eu/webmaster-tips/ruby/ruby-on-rails-formtastic-jquery-ui-datepicker so I:

  1. added jQuery UI (confimed it works with other elements)
  2. installed Formtastic 2.0.2 (confimed it works with other elements)
  3. Adjusted application.js
  4. Adjusted Formtastic.rb as described in the tutorial and the comments
  5. Changed view

I keep getting 'Formtastic::UnknownInputError' on <%= f.input :dtstart, :as => :datepicker %>. I googled and searched for this error but can't find a clue in which direction to look. Anybody here an idea? I'm on rails 3.0.3


Solution

  • if you're using :as => :datepicker, you have to create a custom input for it.

    As the documentation suggests, create a new file in app/inputs named date_picker_input.rb

    class DatePickerInput < Formtastic::Inputs::StringInput
      def input_html_options
        super.merge(:class => "datePicker")
      end
    end
    

    All you have to do is trigger the JQuery UI addon in your application.js:

    $('.datePicker').datepicker();
    

    If you don't want to bother creating a custom input, you can just add the class directly as in:

    <%= f.input :date, :input_html => { :class => 'datePicker'} %>