Giter Club home page Giter Club logo

rails-forms-validations's Introduction

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.

rails-forms-validations's People

Contributors

mtvillwock avatar benhulan avatar

Watchers

James Cloos avatar Annie Redmond avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.