Coder Social home page Coder Social logo

abeytaadam / rails_validations_errors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sf-wdi-24/rails_validations_errors

0.0 2.0 0.0 39 KB

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

Ruby 73.25% JavaScript 2.23% CoffeeScript 0.74% CSS 3.01% HTML 20.77%

rails_validations_errors's Introduction

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 develop folder on your local machine.
  2. Run rake db:create db:migrate in the Terminal to create and migrate your database.
  3. Run rails s in the Terminal to start your server.
  4. Navigate to localhost:3000 in the browser.

Challenges

Model Validations

  1. Add validations to the Pet model. Pets are required to have both name and breed, and name must be at least 3 characters. See the Active Record Validation docs for guidance.

  2. In the Terminal, open up the Rails console, and try adding an invalid pet to the database using the .create method:

irb(main):001:0> pet = Pet.create(name: "Ty")

What happens?

  1. Now try storing the invalid pet in memory with the .new method, and check if it's valid:
irb(main):001:0> pet = Pet.new(name: "Ty")
irb(main):002:0> pet.valid?
  1. Use .errors.full_messages to display the user-friendly error messages for the invalid pet you just created.

Refactor Pets Controller to Handle Errors

  1. The pets#create method currently looks like this:
#
# app/controllers/pets_controller.rb
#
def create
  pet_params = params.require(:pet).permit(:name, :breed)
  pet = Pet.create(pet_params)
  redirect_to pet_path(pet)
end

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

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

  1. Once you've refactored pets#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: Set the flash message in the controller, and render the flash message in the layout (app/views/layouts/application.html.erb).

Stretch Challenges

  1. You already have routes for pets#edit and pets#update, since you're calling resources :pets in routes.rb. Now set up controller methods for pets#edit and pets#update, as well as a view for editing pets (edit form).

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

  3. Read the Rails docs for partials, and use a partial to DRY up the code in new.html.erb and edit.html.erb.

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.