beast学习笔记——5,_head.html.erb
beast学习笔记——5,_head.html.erb
参考:
http://ihower.tw/blog/archives/3949
http://guides.ruby.tw/rails3/i18n.html、
1,
(1)代码
<title>
<%=h @current_site && current_site.name || I18n.t('txt.beast_title', :default => 'Altered Beast') %>
<%= " - #{h @page_title}" if @page_title %>
</title>
(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 => 'Altered Beast')
I18n是从自Rails2.2引入的,用于支持多国语系,对应的语系文档放在 /config/locales 下,使用 yml 格式,默认是en.yml。它的相关应用可以参见http://guides.ruby.tw/rails3/i18n.html。
default方法,表示:当txt.beast_title这个key在yml中找不到键值时,显示“Altered Beast”。
【3】@page_title从何而来??未知??????
2、
(1)代码
<%= stylesheet_link_tag 'display' %>
<%= stylesheet_link_tag 'captcha' %>
(2)表示
表示调用了css样式文件,默认存储位置为:\public\stylesheets下
3
(1)代码
<%= javascript_include_tag "prototype", "effects", "lowpro", "time", "application", :cache => "beast" %>
(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 => true # when ActionController::Base.perform_caching is false => <script type="text/javascript" src="/javascripts/prototype.js"></script> <script type="text/javascript" src="/javascripts/effects.js"></script> ... <script type="text/javascript" src="/javascripts/application.js"></script> <script type="text/javascript" src="/javascripts/shop.js"></script> <script type="text/javascript" src="/javascripts/checkout.js"></script> javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is true => <script type="text/javascript" src="/javascripts/all.js"></script>
javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is false => <script type="text/javascript" src="/javascripts/prototype.js"></script> <script type="text/javascript" src="/javascripts/cart.js"></script> <script type="text/javascript" src="/javascripts/checkout.js"></script>javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is true => <script type="text/javascript" src="/javascripts/shop.js"></script>
4(1)
代码
<% unless @feed_icons.blank? -%>
<% @feed_icons.each do |feed| -%>
<%= auto_discovery_link_tag :atom, feed[:url], :title => "Subscribe to '#{feed[:title]}'" %>
<% end -%>
<% end -%>
(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 # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
auto_discovery_link_tag(:atom) # => <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
auto_discovery_link_tag(:rss, {:action => "feed"}) # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"}) # => <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.currenthost.com/controller/feed" />
auto_discovery_link_tag(:rss, {:controller => "news", :action => "feed"}) # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/news/feed" />
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "Example RSS"}) # => <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed" />
5
(1)代码
<link rel="search" type="application/opensearchdescription+xml" href="http://<%= request.host_with_port %>/open_search.xml" />
(2)表示
添加搜索栏,通过查看源码,得知生成的代码如下:<link rel="search" type="application/opensearchdescription+xml" href="http://localhost:3000/open_search.xml" />
但是,beast并没有open_search.xml啊??????
6
(1)代码
<%= link_to_function I18n.t('txt.search', :default => 'Search'), "#", :href => root_path, :id => 'search-link' %>
(2)表示
link_to_function,会生成一个附有onclick事件的链接,在事件结束后return false(虽然点击该链接的时候不会跳转页面.但是滚动条会往上滚,解决的办法是返回一个false)。上述代码会生成:
<a href="/" id="search-link" onclick="#; return false;">Search</a>
其他例子:
link_to_function "Greeting", "alert('Hello world!')"
Produces:
<a onclick="alert('Hello world!'); return false;" href="#">Greeting</a>
7
(1)代码
<%= link_to I18n.t('txt.signup', :default => 'Signup'), signup_path(:to => CGI.escape(request.request_uri)) %>
(2)表示
上述代码会生成:
<a href="/signup?to=%252F">Signup</a>
首先,了解一下CGI.escape的作用,它用于处理URL中的特殊字符
其次,了解一下模版view的运行环境:控制器中的所有实例变量;通过访问子方法访问控制器中的flash、headers、params、request、response、session对象;通过controller属性可以访问当前控制器;通过base_path属性可以访问当前模板的根路径。
8、
(1)代码
<% name = (site = @current_site || Site.first) && site.name %>
<h1><%= link_to name || I18n.t('txt.beast_title', :default => 'Altered Beast'), root_path %>
(2)表示
暂时的理解,设置变量name并赋值,再建立Beast名称的链接。