Skip to the content

Journal#008 · 2024·02·25

View (Practice) | Ruby on Rails

Practical task for the topic in the View

Task

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 go through a step-by-step solution to our task.

  • Create the Author(name) and Post(title, description, author_id) models.
  • 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 the 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.

 

First step (creating models)

Let's create the authors table in the database.

ruby
rails generate model Author name:string
ruby
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.

ruby
rails generate model Post title:string description:text author:references{foreign_key:true}
ruby
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.

ruby
class Post < ApplicationRecord 
  belongs_to :authorend
end
ruby
class Author < ApplicationRecord
  has_many :postsend
end

 

Second step (Creating MVC for posts)

Let's create a PostsController with the index and show actions.

ruby
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.

ruby
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.

ruby
# app/views/posts/index.html.slimh1 Posts= render partial: 'post', collection: @posts
ruby
# 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.

ruby
# app/views/posts/show.html.slimh1 = @post.titlep = @post.description= link_to 'All posts', posts_path

 

Third step (Creating MVC for authors)

Let's create an AuthorsController with the index and show actions.

ruby
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).

ruby
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.

ruby
# app/views/authors/index.html.slimh1 Authors= render partial: 'author', collection: @authors= link_to 'All posts', posts_path
ruby
# 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.

ruby
# 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

ruby
# app/views/posts/index.html.slim...= link_to 'All authors', authors_path

 

Fourth step (Ability to navigate to the author's page)

Let's provide the ability to navigate to the author's page from the post page.

ruby
# 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

 

Conclusion

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.

rubyco.in