learn-rails icon indicating copy to clipboard operation
learn-rails copied to clipboard

SimpleForm and error_notification

Open simoncal-saas opened this issue 8 years ago • 8 comments

SimpleForm gem was removed and replaced with Rails 5.1 form_with in Book Two Version 4.1.0, Yet, SimpleForm and error_notification method still appear multiple times throughout the book.

simoncal-saas avatar Jul 19 '17 08:07 simoncal-saas

Thanks for letting me know. I thought I caught all the changes. Could you let me know where Simple Form and the error_notification method still appear?

DanielKehoe avatar Jul 19 '17 08:07 DanielKehoe

Searching through Book Two V4.1.0, error_notification showed up 2 times and SimpleForm showed up 16 times.

simoncal-saas avatar Jul 19 '17 08:07 simoncal-saas

Thanks. I'm working on a revision of Chapter 19 the "Contact Form" chapter today and I will check for any residual mentions of Simple Form and error_notification.

DanielKehoe avatar Jul 19 '17 10:07 DanielKehoe

With SimpleForm removed, validation behavior changed. Outdated texts are highlighted below in italic font.

  • Try submitting the form with a blank name. You’ll see a warning message, “Please review the problems below.” The form cannot be submitted.
  • Try submitting the form with an invalid email address such as “me@foo”. The form will re-display with a message, “Please review the problems below,” and next to the email field, “is invalid.” The form cannot be submitted.
  • Combining SimpleForm error handling with ActiveModel validation is powerful. If validation fails after the form is submitted, the page will redisplay and SimpleForm will display an appropriate error message.

I think your intention is for next iteration of code and book to display a flash warning message if @contact.valid? is false, before rendering 'new' template.

simoncal-saas avatar Jul 20 '17 01:07 simoncal-saas

I'm lost at this page, where mention of an error_notification method is made, but there is none visible in the code, and my app doesn't display any errors when I leave out name, email etc on the form.

cmilr avatar Aug 30 '17 20:08 cmilr

@cmilr since SimpleForm gem isn't included, error_detection method is unavailable. Refer to create method in app/controllers/contacts_controller.rb. Since @contact.valid? is false if name, email, or content is missing, the visitor will just be redirected to the same form to fill in the required fields. Hence my above comments "I think your intention is for next iteration of code and book to display a flash warning message if @contact.valid? is false, before rendering 'new' template (again)."

simoncal-saas avatar Aug 31 '17 01:08 simoncal-saas

Thanks, yeah I see that. I'm just hoping to hear for a workaround to get error notifications.

cmilr avatar Aug 31 '17 01:08 cmilr

@cmilr Try this in ContactsController#create method: replace render :new with flash[:alert] = "Missing Name, Email or Message." redirect_to new_contact_path or redirect_to new_contact_path, alert: "Missing Name, Email or Message."

simoncal-saas avatar Aug 31 '17 18:08 simoncal-saas