
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. Also create routes and corresponding views for these actions. On the index page, display all posts, and on the show page, display a single selected post. Create an AuthorsController with index and show actions. On the index page of this controller, display all authors, and on the show page, display information about a specific author and their posts. Provide the ability to navigate to the author's page from the post 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
end
end
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
end
end
Now let's set up our "one-to-many" relationship for these models.
class Post < ApplicationRecord
belongs_to :authorend
end
class Author < ApplicationRecord
has_many :postsend
end
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])
end
end
We display all posts from the database in index, and a separate post by id in show.
Let's create routes for these actions.
Rails.application.routes.draw do root "posts#index" resources :posts, only: %i[index show]end
After that, we need corresponding views for the index and show actions. We display the 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
We display all authors from the database in index, and a separate post by id in show.
Let's also create routes for these actions (routes.rb).
resources :authors, only: %i[index show]
After that, we need corresponding views for the index and show actions. We display the list of authors on the index page.
We display the 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 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 navigate to the authors/index.html.slim page
# app/views/posts/index.html.slim...= link_to 'All authors', authors_path
Let's provide the ability to navigate to the author's page from the post 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. Perhaps you solved it in other ways. If you know how to implement certain parts of our task better, write in the comments.