Skip to content

powercodeacademy/rails-blog-associations-validations

 
 

Repository files navigation

Associations and Validations

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 with a datatype of text.
  2. Create a migration, model, and optionally controller for User and Tag (via bin/rails generate). Check out the documentation on generators, and remember to skip adding tests.
    • users and tags both only have a name column.
  3. In order to create the appropriate associations between Post and Tag, we need to create a join table and model.
  4. Build out model associations and migrations.
    • Posts should OPTIONALLY belong to a user
  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_with(model: @user, local: true) 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 %>

Build out the forms for creating a new 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.

Make the tests in features/tags_for_posts_spec.rb pass.

Resources

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 79.5%
  • HTML 14.0%
  • SCSS 3.0%
  • JavaScript 1.6%
  • CSS 1.4%
  • CoffeeScript 0.5%