简体   繁体   English

Active Admin 剥离表单输入

[英]Active Admin strip form input

Is it possible to strip forms' input fields in Rails Active Admin ?是否可以在 Rails Active Adminstrip表单的输入字段?

eg:例如:

  form do |f|
    f.inputs do
      f.input :attr, as: :string
    end
  end

I tried string_options to copy datepicker_options mechanism, but no luck.我尝试使用datepicker_options复制string_options机制,但没有运气。

  form do |f|
    f.inputs do
      f.input :attr, as: :string, string_options: { strip: true }
    end
  end

I did not found anything in the doc.我在文档中没有找到任何东西。

note : strip means removing empty spaces from the beginning and end of a string.注意:strip 表示从字符串的开头和结尾删除空格。

"  clean input  ".strip # "clean input"

One not recommended way is:一种不推荐的方法是:

f.input :attr, as: :string, html_options: { value: f.object.attr.try(:strip) }

But then you might interfere with formtastic internals, or with localization or someting else.但是你可能会干扰格式内部,或本地化或其他一些东西。 This imho is the better way: use a before validation hook in your ActiveRecord model:这个恕我直言是更好的方法:在您的 ActiveRecord model 中使用之前的验证钩子:

class YourModel < ApplicationRecord
  before_validation :strip_whitespace
   
  def strip_whitespace
    self.attr = self.attr.try(:strip)
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM