Starter code and challenges for Validations & Error-Handling in Rails.
- Fork this repo, and clone it into your WDI class folder on your local machine.
- Run
bundle
in the Terminal to install gems from the Gemfile. - Run
rake db:create db:migrate
in the Terminal to create your local database and run the migrations. - Run
rails s
in the Terminal to start your server. - Navigate to
localhost:3000
in the browser - you should see a genericsite#index
page. - Run
rake routes
to see what routes are available in the app.
-
Run
rspec spec/models/owner_spec.rb
from the Terminal to see the tests that are set up for theOwner
model. -
Add validations to the
Owner
model. Owners have the following restrictions:
- owners are required to have a
first_name
, alast_name
, and anemail
- an owner's
first_name
,last_name
, andemail
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.
- 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?
-
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?
-
Still in the Rails console, use
.errors.full_messages
to display the user-friendly error messages for the invalid owner you just created.
- 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!- 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`).-
You already have routes for
owners#edit
andowners#update
, sinceroutes.rb
callsresources :owners
. Now, set up controller methods forowners#edit
andowners#update
, as well as a view for editing owners (edit form). -
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. -
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. -
If you look at
seeds.rb
, you'll see FFaker is set up to generate seed data. In your Rails console, tryFFaker::PhoneNumber.phone_number
a few times. Just like real user data, the phone numbers don't have a standard format. Fill in thenormalize_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 thenormalize_phone_number
method gets called whenever an owner is about to be saved in the database.