在ActiveAdmin中获取Formtastic以使用自定义表单输入生成器 [英] Getting Formtastic in ActiveAdmin to use custom form input builder

查看:144
本文介绍了在ActiveAdmin中获取Formtastic以使用自定义表单输入生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:因此,我发现了 ,显然这就是为什么这种旧的工作方式无法正常工作的原因,ActiveAdmin必须使用Formtastic2.x。按照指示,我现在在 app / inputs / date_picker_input.rb 中创建了一个文件,如下所示:

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(对于nil:NilClass,未定义方法'html_safe'):
1:渲染renderer_for(:edit)

有什么想法吗?





我遇到了Formtastic自动将日期格式化为我不想要的格式的问题(Ymd h:i: Z)当我尝试渲染:as =>时字符串,这样我就可以在字段上使用日期选择器了。在尝试解决此问题时,我遇到了此解决方案

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.

这似乎很有意义,并且与我正在处理的问题完全相同。但是,我似乎无法实施此修复程序,我想知道是否是因为通过ActiveAdmin使用了Formtastic。因此,这是我尝试做的事情:

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:打开日期,: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

我在 lib / datebuilder.rb 中创建了一个文件,内容如下代码:

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 

我不确定这是否可以修复所需的格式,但是我假设如果我可以让Formtastic使用此方法,则可以根据需要进行更改(当前是从上面链接中提到的解决方案中获取的)。

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).

本文提到您需要在formtastic初始化器中添加一行以使用此自定义输入:

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

Formtastic :: SemanticFormHelper.builder = Formtastic :: DateBuilder

我没有此初始化程序文件放在 config / initializers 中,因此我在上面的行中添加了它( config / initializers / formtastic.rb )。我现在遇到的问题是尝试启动Rails应用程序时出现此错误:

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:在'< top(必需)>'中:未初始化的常量Formtastic :: SemanticFormHelper(NameError)

我也尝试过而是该文件中的语法:

I have also tried this syntax in that file instead:

module Formtastic
  module SemanticFormHelper
    self.builder = DateBuilder
  end
end

这给了我这个错误:。 ./config/initializers/formtastic.rb:3:in'< module:SemanticFormHelper>':未初始化的常量Formtastic :: SemanticFormHelper :: DateBuilder(NameError)

如果我要以完全错误的方式进行操作,请告诉我,否则,让Formtastic使用此自定义输入类型的任何帮助都将是惊人的!

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!

推荐答案

好的,终于找到了正确的方法。

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

我的控制对象ller与更新中的内容相同。但是,这是我将DatePicker定制输入文件( app / inputs / date_picker_input.rb )更改为以下内容的方法:

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!)

这篇关于在ActiveAdmin中获取Formtastic以使用自定义表单输入生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆