Eduardo Eduardo - 18 days ago 5
Ruby Question

Rails redirect_to inside custom class

I have a custom Class in my Rails application, which validates a bunch of settings.

The code:

class UserSettingObject < RailsSettings::SettingObject
validate do

if !/^([0-9]|0[0-9]|1[0]):[0-5][0-9]$/.match(self.time)
redirect_to settings_path, notice: 'Invalid time format'
end

end
end


I check SO posts and found a similar problem (here), the recommendation was
include ActionController::Redirecting
but it doesn't works, resulting in undefined method `config_accessor' error.

How to use Rails redirects methods in a custom Class?

Answer

In addition to what @SergioTulentsev already said, validations are for validating, not for taking actions. What you could do instead is leaving the regexp as a method, and in controller check the time using it and redirect based on the result of the validation.