2011-10-17 89 views
3

我試圖用Formtastic安裝jQuery UI datepicker。我跟着http://blog.brzezinka.eu/webmaster-tips/ruby/ruby-on-rails-formtastic-jquery-ui-datepicker所以我:Ruby on Rails + Formtastic + jQuery UI datepicker

  1. 加入jQuery用戶界面(confimed它與其他元素的作品)
  2. 安裝Formtastic 2.0.2(confimed它與其他元素的作品)
  3. 的application.js調整
  4. 調整Formtastic.rb作爲教程和評論
  5. 更改視圖

我不斷收到「Formtastic ::ü描述nknownInputError'<%= f.input:dtstart,:as =>:datepicker%>。我用Google搜索了這個錯誤,但找不到找到哪個方向的線索。這裏有人有想法嗎?我在軌道上3.0.3

回答

8

如果您使用的是:as => :datepicker,則必須爲其創建自定義輸入。

由於文檔建議,創建app/inputs一個新的文件名爲date_picker_input.rb

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

所有你需要做的是觸發JQuery的UI插件在application.js

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

如果你不」不想創建自定義輸入,您可以直接添加類如下:

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

又一次,從原來的帖子一個遙遠的答案。但事實上,你最終在這裏尋找如何使用Formtastic 3.1和Rails 4來完成這個工作(除此之外可以工作,但未經測試),all I did was look at how ActiveAdmin implements theirs.使用gemfile中的formtastic gem並安裝,你可以將自己的as: :datepicker功能添加到由Formtastic by following the pattern they describe in their documentation使用的input方法。

簡單地說,創建下面的文件/目錄(如果該目錄不存在),並簡單地補充一點:

# <your app>/app/inputs/datepicker_input.rb 
class DatepickerInput < ::Formtastic::Inputs::StringInput 
    def input_html_options 
     super.tap do |options| 
     options[:class] = [options[:class], "datepicker"].compact.join(' ') 
     options[:data] ||= {} 
     options[:data].merge! datepicker_options 
     end 
    end 

    private 
    def datepicker_options 
    options = self.options.fetch(:datepicker_options, {}) 
    options = Hash[options.map{ |k, v| [k.to_s.camelcase(:lower), v] }] 
    { datepicker_options: options } 
    end 
end