add actionsetting / routing
最終更新 2 年前
役に立ちましたか?
/app/controllers/hello_controller.rb
class HelloController < ApplicationController def index render plain:"Hello, This 1s Rails sample page!" end end
routes.rb
Rails.application.routes.draw do get "hello/index" get "hello", to: 'hello#index' end
get "アドレス", to: 'コントローラー#アクション'
get
アドレス", to: 'コントローラー#アクション'
to:=自動的に割り当てるコントローラーやメソッドではなく、実行するアクションメソッドを自分で明示的に指定したい時に設定する。
to:=
http://localhost:3000/hello にアクセスしたら、to: 'hello#index'を呼び出す
http://localhost:3000/hello
to: 'hello#index'
= HelloController クラスのindexメソッドを呼び出す
HelloController
index
<- hello/index 省略可