《Agile Web Development with Rails》读书笔记(三)
来源:互联网 发布:wifi防蹭软件 编辑:程序博客网 时间:2024/05/22 07:00
Make Dynamic Pages
In order to get dynamic pages, I need to embed some ruby code into the *.rhtml in views.
First, I introduce some symbols in *.rhtml.
<%= %> : content between <%= %> is interpreted as ruby code and executed. The result of that execution is converted into a string, and that value is substituted into the file in place of the <%= %> sequence.
<% %> : mostly the same as <%= %>, except the result did not display—never converted into a string and never substituted into the file.
<%= -%> : tell rails to remove any new line that follows from the output.
h( ) : prevent the special characters from grabling the browser display—they’ll escape as html entities.
Now, we create a page to display the current time when you refresh the page.
In order to accord with the spirit of MVC, first we save the current time in a variable in the controller’s method, then we use the variable to display the current time in views.
work/demo/app/controllers/say_controller.rb
class SayController > ApplicationController
def hello
@time = Time.now
end
end
work/demo/app/views/say/hello.rhtml
<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails!</h1>
<p>
It is now <%= @time %>
</p>
</body>
</html>
Save this file, you’ll see the current time when you refresh the page.
Our story illustrates convention over configuration, one of the fundamental parts of the philosophy of Rails.
Link Pages Together
There are two way to link pages together.
<a href = “/say/goodbye”>Goodbye</a>
Rails use a convention to parse the URL into a target controller and an action within that controller. Here say is controller, goodbye is the action in say.
Flaw: the hyperlink path is relative, if we move the application to a different place on the web server, the URL would no longer be valid.
:action : Ruby symbol, you can think of the colon as meaning the thing named…, so :action means the thing named action, the => “goodbye” associate the string goodbye with the name action.
- 《Agile Web Development with Rails》读书笔记(三)
- 《Agile Web Development with Rails》读书笔记(一)
- 《Agile Web Development with Rails》读书笔记(二)
- 《Agile Web Development with Rails》读书笔记(四)
- Agile Web Development with Rails 翻译(三)
- Agile Web Development with Rails
- Agile Web Development with Rails
- Agile Web Development with Rails 翻译(一)
- Agile Web Development with Rails 翻译(二)
- Agile Web Development with Rails 翻译(四)
- Agile Web Development with Rails 翻译(五)
- Agile Web Development with Rails 翻译(六)
- Agile Web Development with Rails 翻译(七)
- Agile Web Development with Rails 翻译(八)
- Agile Web Development with Rails 翻译(九)
- Agile Web Development with Rails 翻译(十)
- Agile Web Development with Rails 翻译(十一)
- Agile Web Development with Rails 翻译(十二 )
- js验证表单大全
- 元格所在的页码通用函数
- 盛大价值理念遭到业内人士质疑
- js验证表单大全 转自wangwang008
- 生产策略 库存生产 净需求计划-10
- 《Agile Web Development with Rails》读书笔记(三)
- [外挂学习]Jim's游戏外挂学习笔记1——动态分配内存的游戏怎么样找内存地址
- [外挂学习]Jim's游戏外挂学习笔记3——继续找当前地图数据和所处坐标存放的地址
- 中科院面试
- Windows常用命令集
- 健康指南:人体十大最佳黄金时间
- C#利用Web Service实现短信发送(2)
- Loki库里的Cyclic Visitor
- 《仙剑》开发商上海软星9月14日正式解散