jeudi 13 août 2015

Rails Validation Failed -- want to stay on form

I have a form in which I am trying to validate that date_to is not less than date_from. The validation seems to be recognized because when I submit a form with date_to less than date_from I get the following error:

Validation failed: Start date must be before end date

However I don't want validation to break the page.

Goal:

If validation fails stay on form page and display a message at the top describing the error.

Model:

validate :validate_date_from_before_date_to, :on => [:create, :edit, :update]

def validate_date_from_before_date_to
  if self.date_from && self.date_to
  errors.add(:end_date, "Start date must be before end date") if self.date_to < self.date_from
end

end

Controller:

if @project.save! == false
  redirect_to edit_admin_project(@project)
else
  redirect_to admin_project_path(id: params[:id])
end

I have checked the similar questions and even one identical one, unfortunately without luck.

Any and all help is greatly appreciated. Thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire