LoginSignup
0
1

More than 5 years have passed since last update.

基礎Ruby on Rails Chapter3 コントローラ

Last updated at Posted at 2018-09-14

基礎Ruby on Rails Chapter1 ディレクトリ構造・コントローラ・アクション・ビューの作成
基礎Ruby on Rails Chapter3 テンプレート

ルーティングの詳細

トップページを指定する。

config/routes.rb
root "home#index"
  • GETメソッドで呼び出す。「get "パス" => "コントローラ名#アクション名"」
  • POSTメソッドで呼び出す。「post "パス" => "コントローラ名#アクション名"」
  • 「コントローラ/アクション名 => "コントローラ名#アクション名"」の場合は、簡略化できる
  • asオプションで名前を付ける。コントローラやビューでhelp_pathというメソッドを呼び出すと"/help"という文字列が返る。
  • 「:パラメータ名」でパラメータをパスの中に埋め込める。showアクションが呼び出され、params[:year]、params[:month]でパラメータが取り出せる。
get "about" => "top#about"
post "login" => "sessions#login"
get "info/company"  # 簡略形式
get "help" => "documents#help", as: "help"  # asオプションで名前を付ける
get "articles/:year/:month" => "articles#show"  # パラメータ埋め込み

コントローラクラスの書き方

ApplicationControllerのサブクラスとする。

top_controller.rb
class TopController < ApplicationController 
  before_action :prepare

  def index
    # アクション。ビューはindex.html.erb
  end

  def about
    # アクション。ビューはabout.html.erb
  end

  private
  def prepare
    # プライベートメソッド
  end

命名規約

  • コントローラの命名規約
    • クラス名 MembersController (〇〇Controller 先頭は大文字)
    • ファイル名 members_controller.rb (〇〇_controller.rb)
    • テンプレートのディレクトリ名 app/views/members (app/views/〇〇)
  • モデルの命名規約
    • テーブル名 members (先頭は小文字。複数形)
    • モデルクラス名 Member (先頭は大文字)
    • モデルクラスのファイル名 member.rb (〇〇.rb)

アクションで使える機能

練習の準備

asagaoプロジェクトにlessonコントローラを追加する。

$ bin/rails g controller lesson

「/lesson/step番号」というパスでLessonControllerのstep1~18までのアクションを呼び出せるようにする。
:nameという名前のパラメータを使えるようにする。

config/routes.rb
Rails.application.routes.draw do
  root "top#index"
  get "about" => "top#about", as: "about"
  # 以下を追加。step1-18までのアクションを呼び出せるようにする。
  1.upto(18) do |n|
    get "lesson/step#{n}(/:name)" => "lesson#step#{n}"
  end
end
  • step1

lesson_controller.rbに、def step1を追加する。

lesson_controller.rb
class LessonController < ApplicationController
  def step1
    render plain: "こんにちは、#{params[:name]}さん"
  end
end

http://localhost:3000/lesson/step1/sato を開く。

image.png

  • step2
lesson_controller.rb
  def step2
    render plain: params[:controller] + "#" + params[:action]
  end

http://localhost:3000/lesson/step2 を開く。

image.png

リダイレクション

  • step3, step4
lesson_controller.rb
  def step3
    # step4にリダイレクトする。(pathでも可)
    redirect_to action: "step4"
    # redirect_to "lesson/step4"
  end

  def step4
    render plain: "step4に移動しました。"
  end

http://localhost:3000/lesson/step3 を開く。

image.png

フラッシュ

  • step5,6
lesson_controller.rb
  def step5
    # リダイレクション後のアクションで文字列を取り出すことができる。
    flash[:notice] = "step6に移動します。"
    redirect_to action: "step6"
  end

  def step6
    render plain: flash[:notice]
  end

http://localhost:3000/lesson/step5 を開く。

image.png

参考
改訂4版 基礎 Ruby on Rails (IMPRESS KISO SERIES)

0
1
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
0
1