|
发现前面写的很乱,今天重新开始一下,前几天学习的也有点乱。
安装配置就不说了下面正式开始代码。
首先创建项目,在终端进入到想创建项目的目录, 输入命令 rails new 项目名称 -d 要使用的数据库 如 mysql
进入项目 这里使用的myfirst cd myfirst
使用命令 rake db:create 命令创建本项目的数据库
使用命令 rails s 启动rails 服务 默认端口3000
使用命令 rails g controller say 创建第一个控制类
编辑say_controller 类 在app/controller目录下
def helloend 在view/say目录下创建 hello.html.erb 文件
<p> this is hello!</p> 编辑 conf目录下的routes.rb
resources :say do #定义say控制类的跳转路由 collection do #使用collection方法进行封装 get 'hello' #使用get方法的访问action end end 现在在浏览器内输入 http://lcoalhost:3000/say/hello
可以看到 this is hello! 成功。
下面 在say_controller.rb文件内编辑
def hello @time = Time.nowenddef goodby end 创建 goodby.html.erb
this is goodby.<br>say <a href="/say/hello"> goto hello</a><br><%=link_to "Hello" , :action => "hello" %> 修改 hello.html.erb
this is hello! time is <%=@time%><BR>say <a href="/say/goodby"> goto goodby! </a><br><%=link_to "Goodby", :action => "goodby" %> #rails 生成动态链接 修改 routes.rb
resouces :say do collection do get 'hello' get 'goodby' endend 这样可以实现简单的跳转,并且hello页面上会打印出时间来。 |
|