GithubHelp home page GithubHelp logo

learn-co-students / rails-blog-associations-validations-denver-web-111819 Goto Github PK

View Code? Open in Web Editor NEW
0.0 26.0 0.0 466 KB

License: Other

Ruby 77.75% JavaScript 1.75% CoffeeScript 0.58% CSS 1.49% HTML 15.16% SCSS 3.28%

rails-blog-associations-validations-denver-web-111819's Introduction

Rails Blog Associations and Validations

This is the second lab iteration off of Rails Blog Scaffold.

Remember: when you generate models, controllers, etc., be sure to include the --no-test-framework option so that it skips tests (which the labs already have).

Objectives:

  • Add a column to an existing table.
  • Generate models, views, and controllers.
  • Create associations between models.
  • Define proper routes.
  • Add validations.

Migrations, Associations, and Routes

  1. Change the migration for posts to include content (set content's datatype to text to account for character length).
  2. Create a migration, model, and optionally controller for User and Tag (via rails generate). Check out the documentation on generators, and remember to skip adding tests.
  3. In order to create the appropriate associations between Post and Tag, we need to create a join table as well.
  4. Build out model associations and migrations.
  5. Be sure to create the appropriate routes. For now, they can be written as resources.
  6. create the database, migrate the schema, and seed it.
  7. Make the model association tests pass.

Validations

Active Record has handy methods you can place on columns in a table to validate certain attributes, like presence, length, and uniqueness. These are called in a model. Check out the documentation to see more.

Let's add validations to...

  1. Post for the presence of both name and content
  2. User for the uniqueness of name
  3. Tag for the uniqueness of name

Active Record handles errors when validations aren't met via user input on our forms. Take a look at what's happening in the partial _form.html.erb for users, which was created when we used Rails's scaffold generator:

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  1. Build out the forms for creating a new post.

Adding tags to a post

You've got a great post on kittens that was generated via the seed file, but you want to be able to add some tags. Create a form on the posts form page that lists all the tags as checkboxes, allowing a user to select multiple tags.

Check out the documentation for the collection_check_boxes form helper here and this post on strong params to be able to make the association between Post and Tag via the Post_Tag join model here.

  1. Make the tests in features/tags_for_posts_spec.rb pass.

Resources

Active Record Validations

Check Boxes

Strong Params Array

rails-blog-associations-validations-denver-web-111819's People

Contributors

ahimmelstoss avatar maxwellbenton avatar irmiller22 avatar changamanda avatar fs-lms-test-bot avatar danielseehausen avatar pletcher avatar vibraniumforge avatar victhevenot avatar fislabstest avatar sarogers avatar roseweixel avatar rrcobb avatar peterbell avatar drakeltheryuujin avatar lizbur10 avatar kthffmn avatar joshuabamboo avatar benjagross avatar aturkewi avatar annjohn avatar

Watchers

 avatar  avatar  avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar  avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  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.