Rails: How to test if an attribute of a class object is required in the model policy?
There isn't a single method you can call to handle this, but this post talks about creating a helper method do to what it sounds like you're after.
module InputHelper
def required?(obj, attribute)
target = (obj.class == Class) ? obj : obj.class
target.validators_on(attribute)
.map(&:class)
.include?(ActiveModel::Validations::PresenceValidator)
end
end
Check out this Railscast about form builder at 2/3 of the cast :
/app/form_builders_labelled_form_builder.rb
def field_label(name, *args)
options = args.extract_options!
required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
label(name, options[:label], class: ("required" if required))
end