undefined method `before_validation'が発生
解決
ActiveModel::Validations::Callbacks
をincludeする
ruby - How can I use before validation callback with form object in Rails 4? - Stack Overflow
class TestForm
include ActiveModel::Model
ActiveModel::Validations::Callbacks
before_validation :format_hogehoge
protected
def format_hoge
if hoge.present?
self.hoge = NKF.nkf('-w --katakana', hoge.gsub(/[\s ]/, ''))
end
end
end
事象
test_form = TestForm.new(params)
test_form.invalid? #<= エラー
class TestForm
include ActiveModel::Model
before_validation :format_hogehoge
protected
def format_hoge
if hoge.present?
self.hoge = NKF.nkf('-w --katakana', hoge.gsub(/[\s ]/, ''))
end
end
end