Skip to content

Starter app for working with Rails views, validations, and error messages.

Notifications You must be signed in to change notification settings

sf-wdi-22-23/rails-forms-validations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rails Forms, Validations, and Error Handling

Exercises

Set up

  1. Clone this repo.
  2. Run rake db:create to make your database.
  3. Start your app with rails server.
  4. Navigate to localhost:3000 – you should see the Owner's index page.

Making Your Pet Model and Migration

Generate a Pet model using rails generate model Pet. This will create a model file like this:

class Pet < ActiveRecord::Base
end

And a corresponding migration file, like this:

class CreatePets < ActiveRecord::Migration
  def change
    create_table :pets do |t|
      # attributes go here
      t.timestamps null: false
    end
  end
end
  1. Give your Pet table a name attribute in the migration using t.string :name. This is similar to how you defined your model schemas in Mongoose.
  2. Add a validation to your pet.rb model that requires the presence of name to be true. Also require name to be at least 3 characters. See the ActiveRecord validation docs for guidance.
  3. Associate your Pet model with an Owner. Use belongs_to :owner.
  4. Generate a new migration to add a foreign key, owner_id to your Pets table. Use rails g migration AddOwnerIdToPet owner_id:integer.
  5. Run rake db:migrate to get your database up to date.

Pets Controller and Routes

  1. Generate a Pets controllers using rails g controller Pets. This will create a file like this:
class PetsController < ApplicationController
  # routing actions go here
end
  1. Define a method in pets_controller.rb called new and a method called create. In both methods, assign an instance variable @pet to Pet.new. Assign an instance variable @owner to Owner.find(params[:owner_id]).

  2. Use an if / else block (after your assignment of @owner and @pet) in PetsController#create to handle valid and invalid form submissions.

  if @pet.save
    @owner.pets << @pet
    redirect_to @owner
  else
    render 'new'
  end
  1. In config/routes.rb, add resources :pets to the resources :owners do ... end block. This will give you access to all seven RESTful routes for Pets.

Making your Pet Form

  1. Create a file in your views/pets directory called new.html.erb.
  2. Use form_for to create a form for @pet.
  3. Add an errors <div> so that an invalid form submission will cause the page to render with the errors displayed.

NOTE: If you need a refresher on syntax for form_for and the errors, refer to the README for examples or look at views/owners/new.html.erb.

If all is right, you should be able to create new pets and associate them with their owners. Additionally, your awesome error handling should display informative messages on how to properly submit your forms. Great job!

Stretch: Make update forms, add more validations

  • In either owners_controller.rb, create edit and update methods, the former renders a form to edit the owner, and later handles the PUT request.
  • Add new attributes to your models with new migrations using rails g migration Add<SOMEATTRIBUTE>To<MODELNAME>. Add validations for these new attributes in the models files (owner.rb and pet.rb). For example:
    • Add a breed attribute to the Pet model by creating a new migration. Add a validation for breed in your models/pet.rb file. Edit your Pet creation form to include the new breed attribute.
    • Add a email attribute to the Owner model by creating a new migration. Add a validation for email in your models/Owner.rb file. Validate the email using a Regular Expression Edit your Owner creation form to include the new email attribute.

About

Starter app for working with Rails views, validations, and error messages.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published