Web Development with Rails and Git Tutorial

Demo 10: Creating Interactive Forms

▶️ Play from the beginning (~1 hour, 10 minutes)

In this demonstration, I will explain how to create interactive forms that access the model perform their function.

▶️ 1. Displaying a Form (that doesn’t yet do anything)

  1. ▶️ Prep: Review a wireframe diagram of the features we aim to build as well as the Rails architecture, giving special attention to the code we’ll need to add/modify.
  2. ▶️ Add a controller for the model class we created in the previous demo (see rails generate controller ...).
  3. ▶️ Add a route to the action that will render the form page.
  4. ▶️ Add a controller action for the form page, retrieving any record(s) needed to create the form.
  5. ▶️ Add a view that uses form_tag to create a form.
    • Oops! In the video, forgot an end statement.
    • Oops! Forgot that some record fields could be nil and should not be listed as radio button options. Fixed in the demo after next.
  6. ▶️ Test and debug: Restart the web server, and inspect the form in a browser.
    • Oops! Missing end bug fixed here in the video.
  7. ▶️ Check in changes: Stage, commit, and push these changes to the Git repo (see git add -A, git commit ..., and git push).

▶️ Check-in Changes: changeset, snapshot

▶️ 2. Processing Submissions of the Form

  1. ▶️ Prep: Review a wireframe diagram of the features we aim to build as well as the Rails architecture, giving special attention to the code we’ll need to add/modify.
  2. ▶️ Add a route to the action that will process the form submission.
  3. ▶️ Add a controller action for the form submission, accessing any record(s) needed to process the form submission and using redirect_to to redirect the browser back to the Home page.
    • Oops! In the video, it should have been include? not includes?.
  4. ▶️ Prep: Check out the Rails guide on the flash: https://guides.rubyonrails.org/action_controller_overview.html#the-flash.
  5. ▶️ Add flash notice/alert messages to the redirects.
    • Oops! The alert message contains a bug. Fixed in the demo after next.

▶️ 3. Adding Flash Messages

  1. ▶️ Prep: Check out the alert styles that Bootstrap provides: https://getbootstrap.com/docs/4.1/components/alerts/.
  2. ▶️ Update application.html.erb to display flash notice/alert messages.
  3. ▶️ Test and debug: Restart the web server, and test out the form in a browser.
    • Oops! include? bug fixed here in the video.
  4. ▶️ Check in changes: Stage, commit, and push these changes to the Git repo (see git add -A, git commit ..., and git push).

▶️ Check-in Changes: changeset, snapshot

▶️ 4. Updating the Home Page and Clean Up Error Handling

  1. ▶️ Update the controller action for the Home page as appropriate.
  2. ▶️ Update the view for the Home page as appropriate. Test and debug as necessary.
  3. ▶️ Clean up error handling (see Ruby exception handling with begin/rescue). Test and debug as necessary.
  4. ▶️ Check in changes: Stage, commit, and push these changes to the Git repo (see git add -A, git commit ..., and git push).

▶️ Check-in Changes: changeset, snapshot

© Scott D. Fleming 2018 • Made with GitHub Pages and Markdown