Ruby on Rails basics

Personal like Ruby

Please let me know if you make a mistake.

Development environment

AWS Cloud9

Ruby on Rails start

It's basically the same as Laravel, and it seems that the idea of MVC is okay.

Environment

$ gem install rails -v 5.2.4
$ rails new app_name

Controller creation

Controller name is plural as much as possible Mostly plural

Location ~ / home / ec2-user / environment / app name / app / controllers

$rails g controller controller name

Command with controller action

$rails g controller controller name action name
$ rails g controller todolists home

Controller deleted

$rails d controller controller name

Routing settings

The way of thinking about routing is the same as Laravel

A place like a command room that controls pretty much anything

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get 'home' => 'tops#home'
#When you come to home with the URL, take the home action of the tops controller! Means
end

View settings

All you have to do is create the file (home.html.erb) specified earlier in the ~ / app / view / name.

I have no choice but to do my best for html and other UI

How to start the server

Either of the following

$ rails server
 $ rails s

I will summarize the error messages someday.

Modeling

A great guy who can instantly connect with database related things.

Check it out for more details (because it's the real thrill of the framework)

$rails g model model name

Singular, first uppercase

OK if 4 files appear

Creation of migration file

It ’s already familiar in Laravel.

I will put a template-like one

class CreateLists < ActiveRecord::Migration[5.2]
  def change
    create_table :lists do |t|
      t.string :name
      t.string :contents
      t.timestamps
    end
  end
end

Migrate

A spell to reflect what is in the migration file in the database

$ rails db:migrate

Column addition after migration

$rails g migration Add Column name To Table name Column name:Model name
$rails g migration AddIdToLists Id:int

Column deletion after migration

$rails g migration Remove Column name From table name Column name:Model name
$rails g migration RemoveIdFromLists Id:int

Well, it's easy to understand if you think about English

But I thought after doing so far. Did you create a database? ?? That?

When I looked it up later, it seems that Ruby on Rails uses SQLite by default instead of the MySQL I usually use ... (I couldn't find it even if I searched for Mysql reasonably).

By the way, the database is

$rake db:create

It seems that this can be done

However, there are still many mysteries, and the momentum was too much ...?

I will update it from time to time.

Recommended Posts

Ruby on Rails basics
Ruby on Rails Elementary
Ruby On Rails Association
Rails basics
Ruby on rails learning record -2020.10.03
Ruby basics
Ruby basics
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Ruby on Rails ~ Basics of MVC and Router ~
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby basics
Ruby on Rails Basic Memorandum
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
Rails CSV basics
Rails Routing Basics
Rails Logger Basics
Tailwind on Rails
Basics of Ruby
Rspec Basics [Rails]
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command