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
这样我们就可以看到我们自己的画面了。