(Ruby on Rails6) Display of the database that got the id of the database

Machine specs

-Version 10.15.3 ・ Ruby ruby 2.6.3p62 ・ Rails 6.0.3.2

Preface

The above recorded how to display the database in the view. However, if you want to make a work with Ruby on Rails, you want to get id </ strong>! Here, the database display by id is recorded as an oblivion record. I hope it helps you with something.

Display of the database that got the id of the database

Please note that you will need to create a database for future work. For more information → To the above

Set id routes

Image of routes If you "post / register" like ↓, you want to create "My Page / Details Page" according to the number of ids. 1 to 4 are ids.

config/routes


Rails.application.routes.draw do
  get 'Controller name/index'
  get 'Controller name/1' => 'Controller name#Action name'
  get 'Controller name/2' => 'Controller name#Action name'
  get 'Controller name/3' => 'Controller name#Action name'
  get 'Controller name/4' => 'Controller name#Action name'
end

If it is left as it is, routes will be described in a large number, so describe it as ↓.

config/routes


Rails.application.routes.draw do
  get 'Controller name/:id' => 'Controller name#Action name'
end

↓ Example

config/routes


Rails.application.routes.draw do
  get 'posts/:id' => 'posts#index'
end

I replaced the parts 1 to 4 with : id </ strong>.

Attention in routes!

If it looks like ↓ on routes, an error will occur.

(error)

config/routes


Rails.application.routes.draw do
  get 'Controller name/:id' => 'Controller name#Action name'← Described first
  get "/" => "forms#index"
end

If you write the : id </ strong> route of get'posts /: id'='posts # index'</ strong> before the normal route, an error will occur. So, write as ↓.

(success)

config/routes


Rails.application.routes.draw do
  get "/" => "forms#index"
  get 'Controller name/:id' => 'Controller name#Action name'← Described later
end

↓ Example

config/routes


Rails.application.routes.draw do
  get "/" => "forms#index"
  get 'posts/:id' => 'posts#index'← Described later
end

Description to controller

The controller is fine with the initial settings at this point.

app/controllers/Any_controller


class PostsController < ApplicationController
def action name
  end
end

Example ↓

app/controllers/Any_controller


class PostsController < ApplicationController
  def index
  end
end

Get id from URL

To get the id, use params [: id] </ strong> for the controller action. params </ strong> is the method that receives the value.

app/controllers/Any_controller


class PostsController < ApplicationController
def action name
    @id = params[:id]
  end
end

Don't forget to use @id as controller must have @.

Example ↓

app/controllers/Any_controller


class PostsController < ApplicationController
  def index
    @id = params[:id]
  end
end

Display in View

erb:app/views/Any/index.html.erb


<h1>Title</h1>
<p>smple text</p>
<%= "id is "#{@id}Is the screen" %>

Let's check with a browser once here

command


rails s

(URL) http://localhost:3000/posts/1

rails-post_test.png

It was displayed. The content of the characters is different between the image and Qiita, but don't worry.

erb:app/views/Any/index.html.erb


<%= "id is "#{@id}Is the screen" %>

The id is read in # {@id} </ strong> of Also, since it is <% = ~~%> </ strong>, it is displayed in the view.

So far, we have set the URL and id. From here, let's get the data corresponding to the id.

Get data corresponding to id

Settings to controller

To output id equal </ strong>

Use find_by and params [: id] </ strong>.

app/controllers/Any_controller


class PostsController < ApplicationController
def action name
    @table name=Database name.find_by(id: params[:id])
  end
end

↓ Example

app/controllers/Any_controller


class PostsController < ApplicationController
  def index
    @post = Post.find_by(id: params[:id])
  end
end

Glossary

find_by </ strong> is getting a value whose id column value is equal to "params [: id]". Also params [: id] </ strong> will be able to hold objects that hold the data sent from the browser to the Rails application.

Settings for views

Finally, set the views. Here, let's display the posted content and posted time.

erb:app/views/Any/index.html.erb


<h1>Title</h1>
<p>sample text</p>
    <%= @post.content %>
    <%= @post.created_at %>

Less than Please enter and check with your browser.

command


rails s

rails-post_test2.png

I typed test1 in the rails console command, and it's reflected.

Element description

erb:app/views/Any/index.html.erb


<%= @post.content %>
→content(Post text)Get

<%= @post.created_at %>
→ Get posting time

Bonus / link

If you want to link to this post, please write the following in views.

erb:app/views/Any/index.html.erb


<%= link_to(Post.content, "/Page name/#{post.id}") %>

Example ↓

erb:app/views/Any/index.html.erb


<%= link_to(post.content, "/posts/#{post.id}") %>

The above code is available on Github. If you are interested, please download it. → Github

Afterword

The above is the display of the database for which the id of the database was acquired. I am very grateful to be able to get the id and output the URL and database contents. If you devise it, you can use it in many situations. Thank you for reading.

Reference link

Books: <a href="https://www.amazon.co.jp/%E3%81%9F%E3%81%AE%E3%81%97%E3%81%84Ruby-%E7%AC%AC6 % E7% 89% 88-Informatics-IDEA-% E9% AB% 98% E6% A9% 8B / dp / 4797399848 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82 % AB% E3% 83% 8A & dchild = 1 & keywords =% E3% 81% 9F% E3% 81% AE% E3% 81% 97% E3% 81% 84Ruby +% E7% AC% AC6% E7% 89% 88 & qid = 1600088731 & s = books & sr = 1-1 "target =" blank "> Fun Ruby 6th Edition

My link

Also, there is a link to the Twitter portfolio, so if you are interested, Please connect. I would be very happy to have friends who can share programming learning.

Twitter Portfolio Github

Recommended Posts