-Version 10.15.3 ・ Ruby ruby 2.6.3p62 ・ Rails 6.0.3.2
In this article, I will record the creation of the database in Ruby on Rails6 and the display in the view as an oblivion. If you are having trouble creating a database, I hope you find it useful.
Here, we will start by creating a directory. The database creation will be described later, so if you know it, you can skip it!
Enter the following command in any directory. Also, since it is supposed to be deployed on heroku, the database (postgresql) is set.
terminal
rails new directory name-TB --database=postgresql
(Example)↓
rails new form_test -TB --database=postgresql
If you don't have a database to set, you can use the following command.
terminal
rails new directory name
(Example)↓
rails new form_test
Let's move to the directory you just created and create a page.
terminal
cd directory name
(Example ↓)
cd form_test
terminal
rails generate controller arbitrary name index
(Example)↓
rails generate controller Forms index
For "index", change the input name with the intention of creating it. There is no problem with the abbreviated command of generate → g. Also, the directory name should be pluralized, such as "Forms", for easier management later.
terminal
rails g controller arbitrary name index
(Example)↓
rails g controller Forms index
If you set the following to routes, it will be set to http: // localhost: 3000 / forms.
config/routes
Rails.application.routes.draw do
# get "Arbitrary name" => "Arbitrary value#index"
get "forms" => "forms#index"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
I will check it with a browser once. Enter the command to display in the browser.
terminal
rails s
or
rails server
Was it displayed?
Also, I wrote it in postgresql directory creation, but I may get an error (I got an error) In that case, enter the following command.
terminal
rails db:create
The cause of the error will be explained in another article.
From here, we will create the database.
terminal
rails g model database name content:text
(Example)↓
rails g model Forms content:text
Database names are easier to manage with the initial "uppercase" and "plural". Not omitted ↓ But OK
terminal
rails generate model database name content:text
(Example)↓
rails generate model Forms content:text
After creating the database, please migrate. If you forget it, you will get an error when checking in your browser.
terminal
rails db:migrate
Instantiate. To do this, type rails console first in the command.
terminal
rails console
Arbitrary name=Database name.new(content: "I'm studying Rails at Progate from today!")
Arbitrary name.save
(Example)↓
form1 = Form.new(content: "test")
form1.save
As an aside, if you enter ↓ when creating a directory, it will be "forms (plural form)", so it is better to set it to "form (singular form)" when adding a database.
terminal
rails generate controller forms index
After adding a post, please save. By doing this, your post will be saved.
terminal
Arbitrary name.save
(Example)↓
form1.save
quit
quit is the command to exit the rails console. You can save multiple posts by repeating this process 2 ~.
terminal
This time i"Directory name=Database name.all"Is used.
Directory name=Database name.all
forms = Form.all
→ Get all values
Directory name[0]
forms[0]
→ Get the first value
Directory name[0].content
forms[0].content
→ Initial value(content)Get
That's all for creating the database. Next, we will work on displaying it in the view.
You can get all the posted contents by setting "directory name = database name.all" used for fetching the database in controller.
Also, in Ruby, "form" must have an @ like "@form".
app/controllers/Any_controller.rb
class PostsController < ApplicationController
def index
# @Directory name=Database name.all
@forms = Form.all
end
end
rb:app/views/Any/index.html.erb
<h1>Forms#index</h1>
<p>Find me in app/views/forms/index.html.erb</p>
<% @Directory name.each do |form| %>
<div>
<%=Singular form of directory name.content %>
</div>
<% end %>
Example ↓
rb:app/views/Any/index.html.erb
<h1>Forms#index</h1>
<p>Find me in app/views/forms/index.html.erb</p>
<% @forms.each do |form| %>
<div>
<%= form.content %>
</div>
<% end %>
rb:app/views/Any/index.html.erb
<% @forms.each do |form| %>
processing
<% end %>
You can iterate by using each. Also
rb:app/views/Any/index.html.erb
<%= form.content %>
Is displaying the "contents" of the database.
And, please note that in Ruby, different behavior occurs due to the difference in the notation of ↓. Here, we want to display the contents of the database, so enter <% = form.content%>.
rb:app/views/Any/index.html.erb
<%= form.content %>
→ Display the character string in the view(=Important)
<% form.content %>
→ Do not display the result of the character string in the view
If there is no problem, you can check it at the route destination. I tried to display test1 and test2 in the test.
The above tests are published on Github. If you want to try it, please download it.
Thank you for reading this far. That's all for creating a database and displaying it in a view in Ruby on Rails6. The routes and controller settings are complicated and a little tiring, but I hope they are displayed in the view.
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
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.