博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ruby on Rails(ROR) 小结(一) 绑定controller and view
阅读量:4051 次
发布时间:2019-05-25

本文共 1112 字,大约阅读时间需要 3 分钟。

view controller 的使用

1, 创建controller, 在工程根目录 下运行如下代码:

ruby script/generate controller meeting

结果: 在app/controllers目录下产生greeting_controller.rb

修改其内容如下:

class GreetingController < ApplicationController

 
  def index
    ##render :text => "<h1>Welcome to your first Rails application</h1>"
    @welcome_message = "Welcome to your first Rails application"  #定义变量
    @age = 8
    @table = { #定义数组
                    'headings' => ['first', 'second', 'three'],
                    'body' => [[1,2,3,], [4,5,6], [7,8,9]]
                  }
  end
end

 

2. 创建视图

ruby script/generate controller meeting index

 结果: 在app/views/greeting目录西安产生 index.html.erb

 

3. 将controller绑定 到view, 修改index.html.erb文件, 调用controller定义的变量

<h1><%= @welcome_message %> <h1>

<h2>simple expression</h2>
<p>Tom is <%= @age %> </p>
<h3>Interation using scriptlets </h3>
<% for i in 1..5 %>
    <p>Handing number is <%= i %></p>
<% end %>
<h1>a simple table</h1>
    <table>
        <tr>
            <% @table["headings"].each do |head|%>    <!--定义临时变量hand, 并遍历数组-->
            <td> 
                <b><%= head %><b/>
            </td>
            <% end %>
        </tr>
        <% @table["body"].each do |row| %>    <!--定义2级临时变量row,col, 并遍历数组-->
        <tr>
            <% row.each do |col| %>
            <td>
                <%= col%>
            </td>
            <% end %>
        </tr>
        <% end %>
    </table>

转载地址:http://ehcci.baihongyu.com/

你可能感兴趣的文章
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
Node.js机制及原理理解初步
查看>>
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>
SIGN UP BEC2
查看>>