Rails是典型的MVC架構,項目的程序主要存在與app這個文件夾中,打開這個文件夾,我們可以看到views和controllers這兩個子文件夾,這個簡單的例子由於沒有用到數據操作,所以暫時與model無關。
我們可以使用generate命令來創建一個新的controller。命令爲:rails generate controller Welcome index
我們可以看到系統自動生成了一些必須的文件,其中我們關注的文件是:
app/controllers/welcome_controller.rb
app/views/welcome/index.html.erb
.
index.html.erb
.是我們看到的頁面的內容,我們將其變更如下:
<h1>Hello, Rails!</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
然後變更一下配置文件,使其路徑匹配。
Rails.application.routes.draw do
get 'welcome/index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'welcome#index'
end
這樣我們就可以看到我們自己的畫面了。