2012-09-17 35 views
2

UPDATE:因此,我發現this,顯然這就是爲什麼這種老辦法不起作用,ActiveAdmin必須使用Formtastic 2.x.作爲導演,我現在已經創建了app/inputs/date_picker_input.rb文件看起來像這樣:在ActiveAdmin中使用Formtastic來使用自定義表單輸入生成器

class DatePickerInput 
    include Formtastic::Inputs::Base 

    def to_html 
    puts "this is my datepickerinput" 
    end 
end 

我的控制器現在看起來是這樣的:

f.input :open_date, :as => :date_picker 
    f.input :close_date, :as => :date_picker 

但現在我遇到了此問題:

ActionView::Template::Error (undefined method 'html_safe' for nil:NilClass): 1: render renderer_for(:edit)

有什麼想法?



我碰到與Formtastic自動格式化日期,格式,我不想要一個問題(YMD H:我:■Z),當我嘗試呈現:as => string,所以我可以在球場上使用日期選擇器。在試圖解決這個問題時,我碰到了this solution

這似乎是有道理的,是我正在處理的完全相同的問題。但是,我似乎無法實現此修復程序,而且我想知道是否因爲通過ActiveAdmin使用了Formtastic。所以,在這裏就是我試圖做的:

在控制器中,我已經改變了方法,例如:

f.input :open_date, :as => :date

我也試過這個,但我的問題是,即使不能夠去了這一點:

f.input :open_date, :as => :date_input

我在lib/datebuilder.rb創建的文件用下面的代碼:

class DateBuilder < Formtastic::SemanticFormBuilder 
    def date_input(method, options) 
    current_value = @object.send(method) 
    html_options ||= {:value => current_value ? I18n.l (current_value) : @object.send("#{method}_before_type_cast")} 
    self.label(method, options.delete(:label), options.slice (:required)) + 
    self.send(:text_field, method, html_options) 
    end 
end 

我不確定這將甚至修復格式,因爲我想,但我認爲如果我可以讓Formtastic使用此方法,我可以根據需要進行更改(目前從上面的鏈接中提到的解決方案)。

This article提到你需要一個行添加到您formtastic初始化程序來使用這個自定義輸入:

Formtastic::SemanticFormHelper.builder = Formtastic::DateBuilder

我沒有這個初始化文件中config/initializers所以我行添加它(config/initializers/formtastic.rb)以上。我現在運行到的問題是試圖啓動Rails應用程序時,這個錯誤:

../config/initializers/formtastic.rb:1:in '<top (required)>': uninitialized constant Formtastic::SemanticFormHelper (NameError)

我也試圖在該文件中,而不是此語法:

module Formtastic 
    module SemanticFormHelper 
    self.builder = DateBuilder 
    end 
end 

這給了我這個錯誤相反:../config/initializers/formtastic.rb:3:in '<module:SemanticFormHelper>': uninitialized constant Formtastic::SemanticFormHelper::DateBuilder (NameError)

如果我以完全錯誤的方式去解決這個問題,請讓我知道,否則任何幫助使Formtastic使用這種自定義輸入類型將是驚人的!

回答

7

好吧,終於想出了正確的方法來做到這一點。

我的控制器和上面的更新一樣。然而,這裏就是我改變了DatePicker的自定義輸入文件(app/inputs/date_picker_input.rb)的樣子:

class DatePickerInput < Formtastic::Inputs::StringInput 
    def to_html 
    "<li class='string input required stringish' id='question_#{method.to_s}_input'>" + 
    "<label class=' label' for='question_#{method.to_s}'>#{method.to_s.gsub!(/_/, ' ').titleize}*</label>" + 
    "<input id='question_#{method.to_s}' name='question[#{method.to_s}]' type='text' value='#{object.send(method)}' class='hasDatePicker'>" + 
"</li>" 
    end 
end 

希望這會幫助別人運行到同樣的問題!順便說一句,硬編碼的「問題」和「需要」是因爲我只會在問題對象上使用這個自定義輸入。有可能有一種方法來動態獲取這些信息,但我決定不要再花費更多的工作來解決這個問題(這足以讓人頭疼!)

相關問題