---
title: " beast学习笔记——5,_head.html.erb"
author: "helloqidi"
date: "2010-12-22T16:38:45Z"
tags: []
categories: ["编程语言"]
slug: "beast-5-head-html-erb"
draft: false
lastmod: "2010-12-22T16:38:45Z"
id: 849491
url: "https://helloqidi.iteye.com/blog/849491"
---

**beast学习笔记——5,\_head.html.erb**

参考：

[http://ihower.tw/blog/archives/3949](http://ihower.tw/blog/archives/3949)

[http://guides.ruby.tw/rails3/i18n.html](http://guides.ruby.tw/rails3/i18n.html)、

[http://apidock.com/rails](http://apidock.com/rails)

1,

(1)代码

&lt;title&gt;

&lt;%=h @current\_site && current\_site.name || I18n.t('txt.beast\_title', :default =&gt; 'Altered Beast') %&gt;

&lt;%= " - #{h @page\_title}" if @page\_title %&gt;

&lt;/title&gt;

（2）表示

【1】current\_site从何而来？

找application\_controller.rb，得知它来自被include的AuthenticatedSystem中。再看authenticated\_system.rb，发现代码：

\# Inclusion hook to make #current\_user and #logged\_in?  
\# available as ActionView helper methods.  
def self.included(base)  
  base.send :helper\_method, :current\_user, :logged\_in?, :current\_site, :admin?, :moderator\_of? if base.respond\_to? :helper\_method  
end

 

其中，self.included这个函式表示：模块AuthenticatedSystem被include时执行。因为是在application\_controller.rb中include的AuthenticatedSystem，所以，每个controller执行时，都会执行self.included方法，所以，每个ActionView都可以使用相应的信息。

其中，send 方法指定方法名称，并调用某个对象的方法。

【2】**I18n.t**('txt.beast\_title', :default =&gt; 'Altered Beast')

I18n是从自Rails2.2引入的，用于支持多国语系，对应的语系文档放在 /config/locales 下，使用 yml 格式，默认是en.yml。它的相关应用可以参见[http://guides.ruby.tw/rails3/i18n.html](http://guides.ruby.tw/rails3/i18n.html)。

default方法，表示：当txt.beast\_title这个key在yml中找不到键值时，显示“Altered Beast”。

【3】**@page\_title从何而来？？未知？？？？？？**

 

2、

（1）代码

  &lt;%= stylesheet\_link\_tag 'display' %&gt;  
  &lt;%= stylesheet\_link\_tag 'captcha' %&gt;

（2）表示

表示调用了css样式文件，默认存储位置为：\\public\\stylesheets下

 

3

（1）代码

  &lt;%= javascript\_include\_tag "prototype", "effects", "lowpro", "time", "application", :cache =&gt; "beast" %&gt;

（2）表示

【1】表示调用了js文件，默认存储位置为：\\public\\javascripts下

【2】cache表示什么？我们可以把多个js文件存储在一个文件中进行缓存，以减少HTTP连接数。此缓存只有在设置ActionController::Base.perform\_caching为true时才起作用。perform\_caching在config\\environments文件夹下的development.rb、production.rb、test.rb文件中，默认情况下，development、test下为false，production下为true。development中的代码为：

config.action\_controller.perform\_caching = false。

具体的区别如下：

  javascript\_include\_tag :all, :cache =&gt; true # when ActionController::Base.perform\_caching is false =&gt;    &lt;script type="text/javascript" src="/javascripts/prototype.js"&gt;&lt;/script&gt;    &lt;script type="text/javascript" src="/javascripts/effects.js"&gt;&lt;/script&gt;    ...    &lt;script type="text/javascript" src="/javascripts/application.js"&gt;&lt;/script&gt;    &lt;script type="text/javascript" src="/javascripts/shop.js"&gt;&lt;/script&gt;    &lt;script type="text/javascript" src="/javascripts/checkout.js"&gt;&lt;/script&gt;  javascript\_include\_tag :all, :cache =&gt; true # when ActionController::Base.perform\_caching is true =&gt;    &lt;script type="text/javascript" src="/javascripts/all.js"&gt;&lt;/script&gt;

javascript\_include\_tag "prototype", "cart", "checkout", :cache =&gt; "shop" # when ActionController::Base.perform\_caching is false =&gt;    &lt;script type="text/javascript" src="/javascripts/prototype.js"&gt;&lt;/script&gt;    &lt;script type="text/javascript" src="/javascripts/cart.js"&gt;&lt;/script&gt;    &lt;script type="text/javascript" src="/javascripts/checkout.js"&gt;&lt;/script&gt;javascript\_include\_tag "prototype", "cart", "checkout", :cache =&gt; "shop" # when ActionController::Base.perform\_caching is true =&gt;    &lt;script type="text/javascript" src="/javascripts/shop.js"&gt;&lt;/script&gt;

 

4（1）

代码

&lt;% unless @feed\_icons.blank? -%&gt;  
  &lt;% @feed\_icons.each do |feed| -%&gt;  
  &lt;%= auto\_discovery\_link\_tag :atom, feed\[:url], :title =&gt; "Subscribe to '#{feed\[:title]}'" %&gt;  
  &lt;% end -%&gt;  
&lt;% end -%&gt;

（2）表示

【1】**@feed\_icons从何而来？来自helper方法**feed\_icon\_tag，在文件application\_helper.rb中

【2】auto\_discovery\_link\_tag，生成一个link标签使浏览器可以使用RSS或ATOM（ATMO可以理解为是对RSS的完善）订阅。通过查看源码，可以发现链接到的控制器为post。

实例：

auto\_discovery\_link\_tag # =&gt;   &lt;link rel="alternate" type="application/rss+xml" title="RSS" href="[http://www.currenthost.com/controller/action](http://www.currenthost.com/controller/action)" /&gt;

 

auto\_discovery\_link\_tag(:atom) # =&gt;   &lt;link rel="alternate" type="application/atom+xml" title="ATOM" href="[http://www.currenthost.com/controller/action](http://www.currenthost.com/controller/action)" /&gt;

 

auto\_discovery\_link\_tag(:rss, {:action =&gt; "feed"}) # =&gt;   &lt;link rel="alternate" type="application/rss+xml" title="RSS" href="[http://www.currenthost.com/controller/](http://www.currenthost.com/controller/feed)[feed](http://www.currenthost.com/controller/feed)" /&gt;

 

auto\_discovery\_link\_tag(:rss, {:action =&gt; "feed"}, {:title =&gt; "My RSS"}) # =&gt;   &lt;link rel="alternate" type="application/rss+xml" title="My RSS" href="[http://www.currenthost.com/controller/feed](http://www.currenthost.com/controller/feed)" /&gt;

 

auto\_discovery\_link\_tag(:rss, {:controller =&gt; "news", :action =&gt; "feed"}) # =&gt;   &lt;link rel="alternate" type="application/rss+xml" title="RSS" href="[http://www.currenthost.com/news/feed](http://www.currenthost.com/news/feed)" /&gt;

 

auto\_discovery\_link\_tag(:rss, "[http://www.example.com/feed.rss](http://www.example.com/feed.rss)", {:title =&gt; "Example RSS"}) # =&gt;   &lt;link rel="alternate" type="application/rss+xml" title="Example RSS" href="[http://www.example.com/feed](http://www.example.com/feed)" /&gt;

 

5

（1）代码

&lt;link rel="search" type="application/opensearchdescription+xml" href="http://&lt;%= request.host\_with\_port %&gt;/open\_search.xml" /&gt;

（2）表示

添加搜索栏，通过查看源码，得知生成的代码如下：&lt;link rel="search" type="application/opensearchdescription+xml" href="[http://localhost:3000/open\_search.xml](http://localhost:3000/open_search.xml)" /&gt;

但是，**beast并没有open\_search.xml啊？？？？？？**

 

6

（1）代码

&lt;%= link\_to\_function I18n.t('txt.search', :default =&gt; 'Search'), "#", :href =&gt; root\_path, :id =&gt; 'search-link' %&gt;

（2）表示

link\_to\_function，会生成一个附有onclick事件的链接，在事件结束后return false（虽然点击该链接的时候不会跳转页面.但是滚动条会往上滚,解决的办法是返回一个false）。上述代码会生成：

&lt;a href="/" id="search-link" onclick="#; return false;"&gt;Search&lt;/a&gt;

其他例子：

link\_to\_function "Greeting", "alert('Hello world!')"

 Produces:

     &lt;a onclick="alert('Hello world!'); return false;" href="#"&gt;Greeting&lt;/a&gt;

 

7

（1）代码

&lt;%= link\_to I18n.t('txt.signup', :default =&gt; 'Signup'), signup\_path(:to =&gt; CGI.escape(request.request\_uri)) %&gt;

（2）表示

上述代码会生成：

&lt;a href="[/signup?to=%252F](http://localhost:3000/signup?to=%252F)"&gt;Signup&lt;/a&gt;

首先，了解一下CGI.escape的作用，它用于处理URL中的特殊字符

其次，了解一下模版view的运行环境：控制器中的所有实例变量；通过访问子方法访问控制器中的flash、headers、params、request、response、session对象；通过controller属性可以访问当前控制器；通过base\_path属性可以访问当前模板的根路径。

 

8、

（1）代码

  &lt;% name = (site = @current\_site || Site.first) && site.name %&gt;  
  &lt;h1&gt;&lt;%= link\_to name || I18n.t('txt.beast\_title', :default =&gt; 'Altered Beast'), root\_path %&gt;

（2）表示

暂时的理解，设置变量name并赋值，再建立Beast名称的链接。