Ruby on Rails for beginners! !! Post list / detailed display function summary

Introduction

Right now, I mainly use php for business, but this time I started learning because I wanted to create something using Rails for self-study. If you have any advice or corrections, please leave a comment.

Development environment

Premise

It is assumed that the Rails development environment has been built and the implementation of the new posting function has been completed. Please implement the new posting function by referring to the previous article. ・ Ruby on Rails for beginners! !! Summary of new posting functions

Post list display function implementation

Add action to controller

First, we will implement the post list function. The list display function basically uses the index action. We will add the index action to the post controller created last time.

ruby:post.controller.rb


 def index

 end

Routing settings

Next is the routing settings.

Add the following description to config/routes.rb.

routes.rb


Rails.application.routes.draw do
  get 'posts/new' => 'posts#new'
  #from here
  get 'posts'     => 'posts#index'
  #Add up to here
  post 'posts'    => 'posts#create'
end

Now you can set "Access the posts path with the GET method will access the index action of the posts controller".

Let's check the settings from the terminal as well. You can check the routing settings with the following command.

# rails routes
posts GET  /posts(.:format)  posts#index                                                               

This completes the routing settings.

Controller settings

Next, I will write the contents of the index action.

posts_controller.rb


def index
    @posts = Post.all
end

To get the post list information, use the all method for the post model. Now, in the instance variable @posts, the posted data list is stored as an array. Make the variable name to be assigned plural.

View settings

Next, we will display the post data acquired earlier in the view. First, create a new index.html.erb file and write the contents.

html:index.html.erb


<h1>Post list</h1>
<% @posts.each do |post| %>
  <p>title</p>
  <span><%= post.title %></span>
<% end %>

<% @posts.each do |post| %>Is an instance variable that stores the post list defined in the controller earlier by iterative processing.@It means that posts are taken out one by one and stored in the block variable post. This makes it possible to use the block variable post, which stores each post in each statement.

The description of <% = post.title%> displays the title columns of the block variable post one by one. You can specify the column to be displayed in "Variable name.Column name".

Now when you access the "/ posts" URL, you'll see a list of post titles. スクリーンショット 2021-01-11 14.57.33.png

Creating a link

Next, from the new post screen, set the link that transitions to the post list screen. Use the link_to method to implement the link. Write the following in the view of the new post screen.

#from here
<span>
    <%= link_to "Post list", "/posts" %>
</span>
#Add up to here
<h1>Post form</h1>
<%= form_for(@post, url: '/posts') do |f| %>
  <h3>title</h3>
  <%= f.text_field :title %>
  <h3>Text</h3>
  <%= f.text_area :body %>
  <%= f.submit 'Post' %>
<% end %>

The linl_to method

<%= link_to text to display, "Link URL" [,option] %>

Describe to use like.

You should now see the text that transitions to the post list page above the post form.

スクリーンショット 2021-01-11 15.27.40.png

This completes the implementation of the post list display function.

Post detail display function implementation

Add action to controller

Next, we will implement the post detail function. Use the show action for the detail display feature. The implementation flow is the same as the list display function. Now let's define the show action in the posts controller.

posts_controller.rb


def show

end

Routing settings

Next is the routing settings.

routes.rb


Rails.application.routes.draw do
  get 'posts/new' => 'posts#new'
  get 'posts'     => 'posts#index'
  #from here
  get 'posts/:id' => 'postss#show'
 #Add up to here
  post 'posts'    => 'posts#create'
end

The URL of the detailed display has an id at the end to identify which detailed data it is. For posts with ID 1, the URL will be something like "posts/1". It can be set with a colon (:) id.

Controller settings

We will describe the contents of the show action.

posts_controller.rb


def show
    @post = Post.find(params[:id])
end

By writing params [: id] in the action, you can get the ID at the end of the URL set in the routing earlier. By using the find method, if id is 1, it means "get one record with ID 1".

View settings

Next, create a view of the detailed display screen. Create show.html.erb and edit the contents.

html:show.html.erb


<h2>title</h2>
<p><%= @post.title %></p>
<h2>Text</h2>
<p><%= @post.body %></p>

You can display the data of that column by the instance variable .column name defined in the show action. This time, title and body are displayed.

Now when the URL accesses "posts/1", the data with id = 1 will be displayed as shown below. スクリーンショット 2021-01-11 21.46.23.png

Creating a link

Finally, from the post list screen, set the link to the detail screen. Rewrite the view on the post list screen as follows.

html:index.html.erb


<h1>Post list</h1>
<% @posts.each do |post| %>
  <p>title</p>
  <span><%= link_to post.title, "/post/#{post.id}" %></span>
<% end %>

Now you can click the title of each post to go to the details page of that post.

スクリーンショット 2021-01-11 21.54.03.png

Summary

--Use the index action for the post list display function and the show action for the detailed display function. --Use the all method on the model to get the data list from the DB. To get specific data, specify id with find method. --Use the link_to method to create a screen transition link.

Recommended Posts

Ruby on Rails for beginners! !! Post list / detailed display function summary
Ruby on Rails for beginners! !! Summary of new posting functions
Explanation of Ruby on rails for beginners ①
Validation settings for Ruby on Rails login function
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] Post image preview function in refile
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Ruby on Rails validation summary
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
[Rails] How to display error messages for comment function (for beginners)
Ruby on Rails Overview (Beginner Summary)
Ruby on Rails variable, constant summary
[Ruby on Rails] Introduced paging function
[Ruby on Rails] CSV output function
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Posting function that only logged-in users can post
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
[Ruby on rails] Implementation of like function
Beginners create portfolio in Ruby on Rails
[Ruby on Rails] Logical deletion (withdrawal function)
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Implementation of Ruby on Rails login function (Session)
[Ruby on Rails] How to display error messages
[Ruby on Rails] Select2 introduction memo for Webpacker
Ruby on Rails Email automatic sending function implementation
[Rails] Procedure for linking databases with Ruby On Rails
[Ruby on Rails] Ranking display (total, average value)
[Ruby on Rails] Individual display of error messages
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
Method summary to update multiple columns [Ruby on Rails]
Implementation of Ruby on Rails login function (devise edition)
[For Rails beginners] Implemented multiple search function without Gem
[Ruby on Rails] Implementation of tagging function/tag filtering function
Ruby on Rails Elementary
Ruby on Rails basics
Ruby on Rails Refactoring method example summary around MVC
Ruby On Rails Association
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
[Ruby on Rails] Search function (model, method selection formula)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Scraping for beginners (Ruby)
<For super beginners> Why don't you make a chatbot using "Talk API"? ?? [Ruby on Rails]
Definitely useful! Debug code for development in Ruby on Rails
Ruby on Rails Email automatic sending function setting (using gmail)
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
(Ruby on Rails6) Display of the database that got the id of the database
From Ruby on Rails error message display to Japanese localization
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Implement login function by add_token_to_users with API
How to display a graph in Ruby on Rails (LazyHighChart)