SlideShare a Scribd company logo
1 of 81
Download to read offline
バカでもわかるRails #03
Model/View/Controller/Routes 編
石井大輔 - www.jenio.co
Twitter@ishiid, email: dai@jenio.co 1
馬鹿
パパ、
Ruby on Rails教えて!
2
題名はクレージーですが、真面目な教材です
Rubyは英語みたいでとっつきやすいのに、Railsはとても難しい。
これが初心者の僕の悩みでした。もっと簡単な学習方法はないものか。
原因はRails用語が実生活で全く使わない抽象概念だからではないでしょうか。
だったらまず用語の意味を繰り返し覚えるべきでは?
英語の勉強と同じ手法を取れば良いのでは?これが僕の仮説です。
Q&A形式でクイズっぽい単語帳カードを作りましたので、
自習もしくは相方と使ってみて下さい。
コーディングを楽しみましょう! 3
単語帳の様に繰り返し学びましょう!
4
注意
この教材は一通りRailsを勉強したが、
記憶が定着していない人を対象としています。
全くの初心者の方は、
まずRubyとRailsを勉強し、補助教材として使ってください。
説明を意図的にシンプルかつ抽象的にまとめてありますので、
細かい点気になる人はググりましょう。
5
“books”という名前のデータベースがあると仮定しましょう。
エクセルシートに形が似ています。
id title price
1 ノルウェイの森 680
2 火花 980
3 サザエさん 540
6
*データベース名は常に小文字で始まり複数形です“books”.
問: Railsが自動生成するデータベースのフィールドは
何?
7
問: Railsが自動生成するデータベースのフィールドは
何?
8
答: id / created_at /
updated_at
問: 結果をBookオブジェクトの配列として返す、book
テーブルからすべてのレコードを無条件に取得するアク
ションは何?
9
問: 結果をBookオブジェクトの配列として返す、book
テーブルからすべてのレコードを無条件に取得するアク
ションは何?
10
答: def list
@books = Book.all
end
問: 自動生成されたbook.rbのコードは何?
11
問: 自動生成されたbook.rbのコードは何?
12
答: class Book <
ActiveRecord::Base
end
*空だが基底クラスActiveRecord::Baseがデータベースにアクセスさせてくれる
問: index.json.jbuilderって何?
13
問: index.json.jbuilderって何?
14
答: indexアクションの結果を
JSON形式で出力する為のテン
プレート
問: @bookの各要素をコードで表示させてください
15
問: @bookの各要素をコードで表示させてください
16
答:
<% @books.each do |book|
%>
問: @bookのtitleをコードで表示させてください
17
問: @bookのtitleをコードで表示させてください
18
答:
<%= book.title %>
問: @bookのpriceをコードで表示させてください
19
問: @bookのpriceをコードで表示させてください
20
答:
<%= book.price %>
問: showへのリンクをコードで表示させてください
21
問: showへのリンクをコードで表示させてください
22
答:
<% link_to ‘Show’, book %>
問: editへのリンクをコードで表示させてください
23
問: editへのリンクをコードで表示させてください
24
答:
<% link_to ‘Edit’,
edit_book_path(book) %>
ビューヘルパー
問: destroyへのリンクをコードで表示させてください
25
問: destroyへのリンクをコードで表示させてください
26
答: <% link_to ‘Destroy’,
book, method: :delete, data:
{ confirm: ‘Are you sure?’
}%>
問: new bookへのリンクをコードで表示させてください
27
問: new bookへのリンクをコードで表示させてください
28
答: <% link_to ‘New Book’,
new_book_path %>
ビューヘルパー
問: booksパスに対応するヘルパーは?
29
問: booksパスに対応するヘルパーは?
30
答:
books_path
問: books/:idパスに対応するヘルパーは?
31
問: books/:idパスに対応するヘルパーは?
32
答:
book_path(:id)
問: books/newパスに対応するヘルパーは?
33
問: books/newパスに対応するヘルパーは?
34
答:
new_book_path
問: books/:id/editパスに対応するヘルパーは?
35
問: books/:id/editパスに対応するヘルパーは?
36
答:
edit_book_path(id)
問: paramsとは何?
37
問: paramsとは何?
38
答: メソッド。params[:id]ではid
パラメーターの値を取得する
問: 共通フォームを表示しなさい、とうコードは何?
39
問: 共通フォームを表示しなさい、とうコードは何?
40
答: <%= render ‘form’ %>
>> _form.html.erbとセットで使
われる。パーシャルという。
問: form_forメソッドの使い方は?
41
問: form_forメソッドの使い方は?
42
答:
<%= form_for(@book) do |f|
%> @book = モデルオブジェ
クト
問: Bookオブジェクトを編集するフォームでtitle列に対応
するテキストボックス
43
問: Bookオブジェクトを編集するフォームでtitle列に対応
するテキストボックス
44
答: <%= f.text_field :title %>
問: 新規登録画面で2つのアクションは?
45
問: 新規登録画面で2つのアクションは?
46
答: new & create
問: newアクションのコードは?新規book入力のため
47
問: newアクションのコードは?新規book入力のため
48
答: def new
@book = Book.new
end
*フォームから入力された情報を格納
問: フォームからの入力値(ポストデータ)をまとめて取得
するには
49
問: フォームからの入力値(ポストデータ)をまとめて取得
するには
50
答: params.require(:book).
permit(:titile, :price………)
問: ハッシュとして入力した値をそのまま変数に渡して下
さい
51
問: ハッシュとして入力した値をそのまま変数に渡して下
さい
52
答:
@book = Book.new
(book_params)
問: tweetsデータベースに対するcontroller/viewのフォ
ルダ、ファイル名を答えてください
53
問: tweetsデータベースに対するcontroller/viewのフォ
ルダ、ファイル名を答えてください
54
答:app/controllers/tweets_controller.rb
app/views/tweets/show.html.erb
問: newメソッドとcreateメソッドの違いは?
55
問: newメソッドとcreateメソッドの違いは?
56
答:new >> formをrenderする getリク
エスト、create >> postリクエスト
問: jsonのkey値をparamsに代入するというコード
57
問: jsonのkey値をparamsに代入するというコード
58
答:return render json: params
*returnを使うと以降の行は飛ばす
render things in json format pass it to params
key = json
問: tweetのtitleを入手するためのparamsは?
59
問: tweetのtitleを入手するためのparamsは?
60
答:params[:tweet][:title]
問: tweetという空のオブジェクトを作ってみて
61
問: tweetという空のオブジェクトを作ってみて
62
答:@tweet = Tweet.new
問: @tweetにtitleを代入してみて
63
問: @tweetにtitleを代入してみて
64
答:@tweet.title = params[:
tweet][:title]
右辺が”default”だとdefaultが代
入される
問: tweetsのmigrationファイルの骨格は?
65
問: tweetsのmigrationファイルの骨格は?
66
答:class
CreateTweets<ActiveRecord::
Migration
end
問: 前ページのmigrationにtitle, bodyの列を設定してみ
て
67
問: 前ページのmigrationにtitle, bodyの列を設定してみ
て
68
答:class CreateTweets<ActiveRecord::Migration
def change
create_table :tweets do |t|
t.string :title
t.text :body
end end end
名前を変えるときは
ここで変える
問: migrationのtableの名前をtweetsに変えるコマンド
は?
69
問: migrationのtableの名前をtweetsに変えるコマンド
は?
70
答:rails g migration
change_name_of_blogposts
問: rollbackとは?使い方は?
71
問: rollbackとは?使い方は?
72
答:前回のmigrationを取り消すこ
と。rake db:rollback
問: dbの初期化、再度整えて使うまでの3つのコマンドは
何?
73
問: dbの初期化、再度整えて使うまでの3つのコマンドは
何?
74
答:rake db:drop
rake db:create
rake db:migrate
migrationはtableを使う上で必須
問: index.html.erbでtweetのtitleとbodyを表示させてみ
て
75
問: index.html.erbでtweetのtitleとbodyを表示させてみ
て
76
答:<% @tweets.each do |tweet| %>
<%= tweet.title %>
<%= tweet.body %>
<% end %>
問: pathがわからない時はどうすれば良い?
77
問: pathがわからない時はどうすれば良い?
78
答:rake routesのprefixを見る。
edit_tweet_path(tweet)
問: destroy/deleteの定義をroutes上で行ってください
79
問: destroy/deleteの定義をroutes上で行ってください
80
答: delete 'tweets/:id/' => "tweets#destroy", as: :
destroy_tweet
GOAL! よくできました!
良かったら繰り返しチャレンジしてみてください。
81
改善の為の、
ご意見ご感想お待ちしています。
Twitter@ishiid
https://twitter.com/ishiid
Email: dai@jenio.co

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

バカでもわかるRails #03