1
0

More than 1 year has passed since last update.

MVCゼミ ワーク

Posted at

ワークの下準備

ターミナルでアプリ作成

ターミナル(コマンドプロンプト)
cd Desktop

rails _6.1.5_ new mvc

cd mvc

code .

rails db:create

# コントローラーを作成してみよう!!

rails g model Post body:string

rails db:migrate

seedを編集しよう!

db/seeds.rb
Post.create([
  { body: 'Web' },
  { body: 'コース' },
  { body: 'LOVE' }
])

ターミナルで適応

ターミナル(コマンドプロンプト)
rails db:seed

MVCを構築していこう!

Controllerにindexアクションを定義してみよう!

app/controllers/posts_controller.rb
class PostsController < ApplicationController
  #追加する箇所
 

  #ここまで
end

Viewに@postsを表示してみよう!

app/views/posts/○○○.html.erb
<h1>MVCゼミ</h1>
<h3>MVC一覧</h3>
<div class="MVC-container">
  <% @posts.each do |t| %>

  #追加する箇所
 

  #ここまで

  <% end %>
</div>

Routesにposts/index のURLが送られたら routes.rb で posts_controller.rb の index が実行

config/routes.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

  #追加する箇所
 

  #ここまで

end

つまずいた人は以下をクリック!

つまずいた人向け

Controllerにindexアクションを定義してみよう!

app/controllers/posts_controller.rb
class PostsController < ApplicationController
  #追加する箇所

  #ヒント
  def index
    @posts = ○○○.○○○
   end

  #ここまで
end

Viewに@postsを表示してみよう!

app/views/posts/○○○.html.erb
<h1>MVCゼミ</h1>
<h3>MVC一覧</h3>
<div class="MVC-container">
  <% @posts.each do |t| %>

  #追加する箇所
 
  #ヒント:<%= t.○○○ %>

  #ここまで

  <% end %>
</div>

Routesにposts/index のURLが送られたら routes.rb で posts_controller.rb の index が実行

config/routes.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

  #追加する箇所

  #ヒント:get '○○○/○○○' => '○○○#○○○'

  #ここまで

end
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0