Getting Formtastic in ActiveAdmin to use custom fo

2019-07-11 02:41发布

问题:

UPDATE: So, I've found this, and apparently that's why this old way of doing things isn't working, ActiveAdmin must use Formtastic 2.x. As directed, I've now created a file in app/inputs/date_picker_input.rb that looks like this:

class DatePickerInput
  include Formtastic::Inputs::Base

  def to_html
    puts "this is my datepickerinput"
  end
end

And my controller now looks like this:

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

But now I'm running into this error:

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

Any thoughts?



I've ran into a problem with Formtastic automatically formatting dates into a format I'm not wanting (Y-m-d h:i:s Z) when I try to render :as => string so I can use a datepicker on the field. In trying to solve this, I came across this solution.

It seems to make sense and is the exact same problem I am dealing with. However, I don't seem to be able to implement the fix, and I'm wondering if it's because Formtastic is being used through ActiveAdmin. So, here's what I've tried to do:

In the controller, I've changed the method as such:

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

I also tried this, though my problem is not even being able to get to this point:

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

I created a file at lib/datebuilder.rb with the following code:

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 

I'm not sure that will even fix the format as I want, but I assume if I can just get Formtastic to use this method I can alter it as needed (currently took this from the solution mentioned in link above).

This article mentions you need to add a line to your formtastic intializer to use this custom input:

Formtastic::SemanticFormHelper.builder = Formtastic::DateBuilder

I did not have this initializer file in config/initializers so I added it (config/initializers/formtastic.rb) with the line above. The problem I am now running into is this error when trying to startup the Rails app:

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

I have also tried this syntax in that file instead:

module Formtastic
  module SemanticFormHelper
    self.builder = DateBuilder
  end
end

which gives me this error instead: ../config/initializers/formtastic.rb:3:in '<module:SemanticFormHelper>': uninitialized constant Formtastic::SemanticFormHelper::DateBuilder (NameError)

If I'm going about this in completely the wrong way please let me know, otherwise any help on getting Formtastic to use this custom input type would be amazing!

回答1:

Alright, finally figured out the right way to do this.

My controller stayed the same as seen above in the update. However, here's what I changed the DatePicker custom input file (app/inputs/date_picker_input.rb) to look like:

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

Hopefully this will help someone else running into the same problem! Btw, the hard-coded "question" and "required" is because I will only use this custom input on question objects. There is likely a way to dynamically get this information, but I decided against putting more work in to figure that out (this was enough of a headache on its own!)