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.
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
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
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.
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.
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.
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.
This completes the implementation of the post list display function.
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
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.
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".
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.
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.
--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