
Models (Practice) | Ruby on Rails
Practical task for the topic in the Model

Create the models Author(name) and Post(title, description, author_id). Create a PostsController with two actions: index and show. For these actions, also create routes and the corresponding views. On the index page, display all posts, and on the show page — a single selected post. Create an AuthorsController with actions index and show. On this controller’s index page, display all authors, and on the show page — information about a specific author and their posts. Provide the ability to navigate to an author’s page from a post’s page.
Let’s create the authors table in the database.
rails generate model Author name:string
class CreateAuthors < ActiveRecord::Migration[7.1] def change create_table :authors do |t| t.string :name t.timestamps end endend
Let’s create the posts table in the database.
rails generate model Post title:string description:text author:references{foreign_key:true}
class CreatePosts < ActiveRecord::Migration[7.1] def change create_table :posts do |t| t.string :title t.text :description t.references :author, foreign_key: true t.timestamps end endend
Now let’s set up our "one-to-many" association for these models.
class Post < ApplicationRecord belongs_to :authorend
class Author < ApplicationRecord has_many :postsend
Let’s create a PostsController with the index and show actions.
class PostsController < ApplicationController def index @posts = Post.all end def show @post = Post.find(params[:id]) endend
In index we output all posts from the database, and in show we output a single post by id.
Create routes for these actions.
Rails.application.routes.draw do root "posts#index" resources :posts, only: %i[index show]end
After that, we need the corresponding views for the index and show actions. Display a list of posts on the index page.
# app/views/posts/index.html.slimh1 Posts= render partial: 'post', collection: @posts
# app/views/posts/_post.html.slim.post h1 = post.title p = post.description h5 = post.author.name = link_to 'View', post_path(post.id)
In the show.html.slim view, we can access a single post.
# app/views/posts/show.html.slimh1 = @post.titlep = @post.description= link_to 'All posts', posts_path
Let’s create an AuthorsController with the index and show actions.
class AuthorsController < ApplicationController def index @authors = Author.all end def show @author = Author.find(params[:id]) @posts = @author.posts endend
In index we output all authors from the database, and in show we output a single post by id.
Create routes for these actions as well (routes.rb).
resources :authors, only: %i[index show]
After that, we need the corresponding views for the index and show actions. Display a list of authors on the index page.
Display a list of authors on the index page.
# app/views/authors/index.html.slimh1 Authors= render partial: 'author', collection: @authors= link_to 'All posts', posts_path
# app/views/authors/_author.html.slim.author h1 = author.name = link_to 'View', author_path(author.id)
In the show.html.slim view, we can access a single post.
# app/views/authors/show.html.slimh1 = @author.name- if @posts h3 List of the author's posts = render partial: 'posts/post', collection: @posts= link_to 'Go back', authors_path
Also, let’s provide the ability on the posts/index.html.slim page to go to the authors/index.html.slim page
# app/views/posts/index.html.slim...= link_to 'All authors', authors_path
Provide the ability to navigate to an author’s page from a post’s page.
# app/views/posts/show.html.slimh1 = @post.titlep = @post.descriptionspan Author: = link_to @post.author.name, author_path(@post.author_id)br= link_to 'All posts', posts_path
So, this is one of the solutions to our task. You may have solved it in other ways. If you know how to implement certain parts of our task better, then write in the comments.