Eduardo Eduardo - 1 year ago 58
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'


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download