After moving to the directory you want to create
Terminal
rails new [app name] -d mysql
Specify mysql when creating database with "-d mysql"
Move to the created app directory. The following command will create the database according to the settings described in database.yml.
Terminal
rake db:create
If it doesn't work as expected, check database.yml. Since sqlite is specified in the initial database, if necessary, write mysql in gem and bundle install.
Reference: Let's develop Rails applications! ~ Development Preparation ~
Terminal
rails g controller [Controller name]
routes.rb
root "[Controller name]#index"
When root accesses with url, the controller and action displayed on the top page are displayed. This time, the [index] action is specified.
I will describe the necessary actions.
[Controller name]_controller.rb
def index
end
Since the [app/views/controller name /] directory has been created, create the index.html.erb file.
html:index.html.erb
<p>Hello, World!</p>
Here, when you start the rails server and access it locally, "Hello, World!" Is displayed.
Terminal
rails g model [Model name]
When you create a model, a migration file for creating a table is automatically generated. Describe the migration file and execute the migration file.
Terminal
rails db:migrate
Reference Active Record Migration Reference Rails Migration Basic Summary
that's all.
Recommended Posts