目录:简学互动rails入门
准备工作目录
mkdir jxhd-rails
cd jxhd-rails
生成应用hello-1
环境安装参考Ruby-china的wiki。
rails new hello-1
系统要安装一些软件,所以要等一会儿,大概几分钟。
生成的hello-1目录就是主目录。可以浏览下内容,后面的目录都以主目录为参照。
里面有个app,是关键目录。rails是用的MVC模式,也就是Model,View,Controller。
操作步骤
1 先来看看这个MVC的C
app下controllers目录里我们建立一个文件:
hello_controller.rb
内容如下:
class HelloController < ApplicationController
def index
end
end
2 简单处理路由
调出文件config/route.rb,在最后的end前加上这一行
match ':controller(/:action(/:id))(.:format)', via: [:get, :post]
3 再看看MVC的V
app/views下建立一个目录hello,再在hello里建立文件index.erb,内容是:
hello world
看看结果
启动服务器
rails s
好了,可以收工了,去浏览器看看。
看到了吗,hello world
最后
简单解释一下,浏览器里的/hello,会对应到controlloer的hello,默认是到index,目前是空的,然后会对应到view的目录中的hello下的index.erb。
网友评论