For this weekend, we'd like you to start building the foundation for a blog. Your blog will have full CRUD on two resources, authors and posts. The association you should keep in mind is that one author can write many posts.
- Rails Routing from the Outside In
- Read Nested Resources, especially 2.7.2 Shallow Nesting
- Active Record Associations
- Read ActiveRecord::Associations::ClassMethods
- Especially, focus on understanding "Deleting from associations"
- Read Foreign Keys
- Active Record Validations
- Generate the rails application
- Generate your models
- authors should have a first_name, last_name and age
- posts should have a title, body, category and author_id
- one author has many posts, and when an author is deleted, the author's posts should be deleted as well.
- Set up your associations and validations
- all authors must have a first_name and last_name
- first_name and last_name should be between 4 and 20 characters long
- age must be a number
- all posts must have a title, body and category
- the title must be at least 3 characters long
- Test everything in Rails Console
-
if you have set up your associations correctly, you should be able to type
a = Author.create(first_name: "Elie", last_name: "Schoppik", age: 26) a.posts # this should not throw an error p = Post.create(title: "First post", body: "Awesome post", category: "none", author_id: 1) p.author # this should not throw an error either
-
- Generate your controllers and views
- make sure you give both of your resources full CRUD
- Refactor your routes to use resources
- since you have a model that belongs to another, you will be using nested resources.
- Start working on the controllers
- Make sure you are displaying any errors upon creation/updating as well as flash messages when data is added/changed
- All of your views should use Rails form and link helpers. You should not be writing
<form>
or<a>
tags at all!
- Once your application is working, add bootswatch and style the app!
- Add some interactivity to your page!