samadrid92 / rails_validations_errors

Starter code and challenges for validations & error-handling in Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rails Validations & Error-Handling

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

Getting Started

  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.
  6. 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.

  1. In the Terminal, open up the Rails console, and try adding an invalid owner to the database using the .create method:
irb(main):001:0> guy = Owner.create({
irb(main):001:1>   first_name: "Guybrush",
irb(main):001:2>   last_name: "Threepwood"
irb(main):001:3> })

What happens?

  1. Now try storing the invalid owner in memory with the .new method, and check if it's valid:

    irb(main):001:0> guy = Owner.new({
    irb(main):001:1>   first_name: "Guybrush",
    irb(main):001:2>   last_name: "Threepwood"
    irb(main):001:3> })
    irb(main):001:4> guy.valid?
  2. Still in the Rails console, use .errors.full_messages to display the user-friendly error messages for the invalid owner you just created.

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

Starter code and challenges for validations & error-handling in Rails


Languages

Language:Ruby 75.4%Language:HTML 18.1%Language:CSS 3.2%Language:JavaScript 1.7%Language:CoffeeScript 1.6%