fishcharlie / rails-validations-errors-lab

[rails, errors, validations]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#Rails Validations & Error-Handling Lab

Starter code and challenges for Validations & Error-Handling in Rails.

Getting Started

See if you can fork and clone this repo, and get a Rails server running. If you need a hint, expand this, but try it once without looking if you can.
  1. Fork this repo, and clone it into your WDI class folder on your local machine.
  2. Run `bundle` in the Terminal to install gems from the Gemfile.
  3. Run `rake db:create db:migrate` in the Terminal to create your local database and run the migrations.
  4. Run `rails s` in the Terminal to start your server.
  5. Navigate to `localhost:3000` in the browser - you should see a generic `site#index` page.

Finally, run rake routes to see what routes are available in the app.

Challenges

Add Model Validations

  1. Run rspec spec/models/owner_spec.rb from the Terminal to see the tests that are set up for the Owner model.

  2. Add validations to the Owner model. Owners have the following restrictions:

  • owners are required to have a first_name, a last_name, and an email
  • an owner's first_name, last_name, and email must each be at most 255 characters long
  • emails must be unique; that is, no two owners can have the same email
  • emails must contain an @ symbol

See the Active Record Validation docs for guidance.

Refactor Controller to Handle Errors

  1. The owners#create method currently looks like this:
#
# app/controllers/owners_controller.rb
#
def create
  owner = Owner.create(owner_params)
  redirect_to owner_path(owner)
end

The owner_params method is set up to use Rails strong parameters.

What happens when you navigate to localhost:3000/owners/new in the browser and try to submit a blank form?

Refactor your owners#create controller method to better handle this error. Hint: Use .new and .save.

Hint: which methods to use? Try `.new` and `.save` so it's easier to see if there's an error.
Hint: what to do if there's an error? For now, redirect back to the page with the form. If you don't remember the path helper method to use, `rake routes` in your Terminal and check the prefixes!
  1. Once you've refactored owners#create to redirect in the case of an error, add flash messages to show the user the specific validation error they triggered, so they won't make the same mistake twice.
Hint: where to set the flash message? Set the flash message in the controller by adding the message into the `flash` hash. (See the [Rails Flash message docs](http://api.rubyonrails.org/classes/ActionDispatch/Flash.html) for a syntax refresher.)
Hint: where to display the flash message? Create a place to render the flash message in the main application layout (`app/views/layouts/application.html.erb`).

Stretch Challenges

  1. You already have routes for owners#edit and owners#update, since routes.rb calls resources :owners. Now, set up controller methods for owners#edit and owners#update, as well as a view for editing owners (edit form).

  2. Make sure your owners#update method also handles errors by redirecting if the user submits invalid data and displaying a flash message in the view.

  3. Common keys for flash messages are :notice, which just displays some information, and :error, which means something has gone wrong. Add styling to visually distinguish between these kinds of flash messages.

  4. If you look at seeds.rb, you'll see FFaker is set up to generate seed data. In your Rails console, try FFaker::PhoneNumber.phone_number a few times. Just like real user data, the phone numbers don't have a standard format. Fill in the normalize_phone_number method so that it will:

  • remove 1 from the beginning of the owner's phone number, and
  • remove the characters (, ), -, and . from the owner's phone number.

The before_save method makes it so the normalize_phone_number method gets called whenever an owner is about to be saved in the database.

About

[rails, errors, validations]


Languages

Language:Ruby 78.1%Language:HTML 16.1%Language:CSS 2.8%Language:JavaScript 1.5%Language:CoffeeScript 1.5%