---
# System prepended metadata

title: Tutorial of Ruby on Rails

---

# Tutorial of Ruby on Rails

[Getting Started with Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/getting_started.html#what-is-rails-questionmark)

## 5.1 Laying down the groundwork

> A controller is simply a class that is defined to inherit from ApplicationController. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the articles within our system.

> There are public, private and protected methods in Ruby, but **only public methods can be actions for controllers.**

FluxでActionにしか外からの命令を受け取れないのと同じような仕組み？

→ いえ、単に他の言語と同様 `private` や `protected` の概念があって、他から呼ばれたくないのは `public` にするなよ、と言うだけです。

## 5.4 Creating the Article model

> Models in Rails use a singular name, and their corresponding database tables use a plural name.

命名に気を配る。

→ そうです、俺が「これ、単数形だよね？複数形じゃないよね？」とかうるさいのはこれが原因です。

## 5.5 Running a Migration

> `rails db:migrate`

> Rails will execute this migration command and tell you it created the Articles table.

> Because you're working in the development environment by default, this command will apply to the database defined in the development section of your config/database.yml file.

SQLite3がRequirementにあったけど、`rails new blog` しただけで、データベースも最初から使えるようになってるのか。

→ 「最初？」日本語ちゃんと書いて。

→ （なぜか消えていた。書き直したけど、自分ではほとんど準備した感のないデータベースにすぐデータ保存できてしまったことに驚いた）

> why the A in Article.new is capitalized

> In this context, we are referring to the class named Article that is defined in `app/models/article.rb` . Class names in Ruby must begin with a capital letter.

これは特に違和感ないけど、メモ。

## 5.7 Showing Articles

> A frequent practice is to place the standard CRUD actions in each controller in the following order: **index, show, new, edit, create, update and destroy**.

## 5.9 Adding links

> Open `app/views/welcome/index.html.erb` and modify it as follows:
> `<%= link_to 'My Blog', controller: 'articles' %>`

> If you want to link to an action in the same controller, you don't need to specify the `:controller` option, as Rails will use the current controller by default.

他のところのリンクと書き方違うと思ったら、そういうことか。

## 5.12 Using partials to clean up duplication in views

> By convention, **partial files are prefixed with an underscore**.
---
> `<%= form_with model: @article, local: true do |form| %>`

の`model: @article` と置き換えられる理由について

> ### Resource-oriented style
> #### form_with options
> :model - A model object to infer the :url and :scope by, plus fill out input field values.

## 5.13 Deleting Articles

> Note that we don't need to add a view for this action since we're redirecting to the index action.

## 6.1 Generating a Model

> The (:references) keyword used in the bash command is a special data type for models. It creates a new column on your database table with the provided model name appended with an _id that can hold integer values.

## 7.1 Rendering Partial Collections

from

> `<% @article.comments.each do |comment| %>`
> `…`
> `<% end %>`

to

> `<%= render @article.comments %>` 

with `app/views/comments/_comment.html.erb`

# The Ruby Style Guide

cf. [rubocop-hq/ruby-style-guide: A community-driven Ruby coding style guide](https://github.com/rubocop-hq/ruby-style-guide)