Linking to Other Pages and Resources

本文介绍了Rails框架中用于创建链接、按钮、图片标签及邮件链接的方法,如link_to、button_to、image_tag等,并展示了如何利用这些方法来增强应用程序的功能。
ActionView::Helpers::AssetTagHelper 和ActionView::Helpers::UrlHelper 模块包
含很多方法,它们可让你引用外部资源到当前“模板”中。当然,更通常使用的是link_to(),
它创建一个超链接给你的应用程序内的其它“动作”。
<%= link_to "Add Comment", :action => "add_comment" %>
传递给link_to()的第一个参数是用于显示这个链接的文本。下一个是指定链接目标的
哈希表。它们的格式与284 页中讨论的“控制器”的url_for()方法一样。第三个参数可以
用于设置被生成的链接的HTML 属性。这个属性支持一个额外的key 键,:confirm,它的值是
个短消息。如果出现的话,JavaScript 将被生成以显示此消息,并在链接之前接受用户的确
认。
<%= link_to "Delete", { :controller => "admin",
:action => "delete",
:id => @product
},
{ :class => "redlink",
:confirm => "Are you sure?"
}
%>
button_to()方法工作像link_to()一样,但生成个self-contained 窗体的按钮,而不
是一个平淡的链接。像我们在324 页16.9 节讨论的,这是链接带有副作用的“动作”的首选
方法。尽管这些按钮有它们自己的格式,它有很多重要的约束:它们不能内联,不能出现在
其它窗体内。
还有几个有条件连接方法,如果一些条件达到了,这些方法就生成链接,否则的话就返
回链接的文本内容。Link_to_unless_current()“帮助方法”对在sidebars 内创建菜单很有
用,在sidebar 内当前页的名字被显示为纯文本并且其它条目是超链接。
<ul>
<% %w{ create list edit save logout }.each do |action| -%>
<li>
<%= link_to_unless_current(action.capitalize, :action => action) %>
</li>
<% end -%>
</ul>
就像url_for( ), link_to( ) 和类似方法也支持绝对URL。
<%= link_to("Help", "http://my.site/help/index.html") %>
image_tag( ) “帮助方法”可以用于创建<img> 标记。
<%= image_tag("/images/dave.png", :class => "bevel", :size => "80x120")
%>
如果image 路径不包括一个”/”反斜线字符,则Rails 假设它放在/images 目录中。如
果没有文件扩展名,Rails 假设是.png。下面的代码与前面的例子是一样的。
<%= image_tag("dave", :class => "bevel", :size => "80x120") %>
你可以通过结合link_to()和image_to()把图像放到链接内。
<%= link_to(image_tag("delete.png", :size="50x22"),
{ :controller => "admin",
:action => "delete",
:id => @product },
{ :confirm => "Are you sure?" })
%>
mail_to()“帮助方法”创建一个mailto:超链接,在单击时,通常会加载客户端的e-mail
应用程序。它接受一个e-mail 地址,链接的名字,和一组HTML 选项。在些选项中,你也可
以使用:bcc,:cc,:body,和:subject 来初始化相应的email 字段。最后,魔术选
项:encode=>”javascript”使用客户端JavaScript 来暗中生成链接,这会让spiders 的搜
索引擎很难从你的站点中找到e-mails 地址。[但是也意味着如何你的浏览器关闭了
JavaScript 的话,你就不能看到e-mails 链接。]
<%= mail_to("support@pragprog.com", "Contact Support",
:subject => "Support question from #{@user.name}",
:encode => "javascript")
%>
AssetTagHelper 模块也包括这样的“帮助方法”,它可以轻易地让你的页连接样式表和
JavaScript 代码。我们在Depot 应用程序的“层”内,创建一个样式表连接,在head 中我
们使用stylesheet_link_tag()。
<%= stylesheet_link_tag "scaffold", "depot", :media => "all" %>
一个RSS 或Atom 链接是个header 字段,它指向我们应用程序的一个链接。当URL 被访
问时,应用程序应该返回相应的RSS 或Atom XML。
<html>
<head>
<%= auto_discovery_link_tag(:rss, :action => 'rss_feed') %>
</head>
. . .
最后,JavaScriptHelper“模型”定义许多“帮助方法”用于JavaScript 的工作。这些
create JavaScript 片断运行在浏览器中以生成特定的效果,并且可让页动态地与我们的应
用程序交流。这个主题在373 页的18 章自成一章。
缺省地,image 和stylesheet assets 放在运行的应用程序的/images 和/stylesheets
目录内。如果路径给出一个包含前反斜线的asset tag 方法,那么路径被认为是绝对的,并
且没有使用前缀。有时候它会移却它的静态内容到一个单独的box 内或是当前box 内的别的
位置。可通过设定配置变量asset_host 来做到。
ActionController::Base.asset_host = "http://media.my.url/assets"
Building a Busybox Boot Floppy ============================== This document describes how to buid a boot floppy using the following components: - Linux Kernel (http://www.kernel.org) - uClibc: C library (http://www.uclibc.org/) - Busybox: Unix utilities (http://busybox.net/) - Syslinux: bootloader (http://syslinux.zytor.com) It is based heavily on a paper presented by Erik Andersen at the 2001 Embedded Systems Conference. Building The Software Components -------------------------------- Detailed instructions on how to build Busybox, uClibc, or a working Linux kernel are beyond the scope of this document. The following guidelines will help though: - Stock Busybox from CVS or a tarball will work with no modifications to any files. Just extract and go. - Ditto uClibc. - Your Linux kernel must include support for initrd or else the floppy won't be able to mount it's root file system. If you require further information on building Busybox uClibc or Linux, please refer to the web pages and documentation for those individual programs. Making a Root File System ------------------------- The following steps will create a root file system. - Create an empty file that you can format as a filesystem: dd if=/dev/zero of=rootfs bs=1k count=4000 - Set up the rootfs file we just created to be used as a loop device (may not be necessary) losetup /dev/loop0 rootfs - Format the rootfs file with a filesystem: mkfs.ext2 -F -i 2000 rootfs - Mount the file on a mountpoint so we can place files in it: mkdir loop mount -o loop rootfs loop/ (you will probably need to be root to do this) - Copy on the C library, the dynamic linking library, and other necessary libraries. For this example, we copy the following files from the uClibc tree: mkdir loop/lib (chdir to uClibc directory) cp -a libc.so* uClibc*.so \ ld.so-1/d-link/ld-linux-uclibc.so* \ ld.so-1/libdl/libdl.so* \ crypt/libcrypt.so* \ (path to)loop/lib - Install the Busybox binary and accompanying symlinks: (chdir to busybox directory) make CONFIG_PREFIX=(path to)loop/ install - Make device files in /dev: This can be done by running the 'mkdevs.sh' script. If you want the gory details, you can read the script. - Make necessary files in /etc: For this, just cp -a the etc/ directory onto rootfs. Again, if you want all the details, you can just look at the files in the dir. - Unmount the rootfs from the mountpoint: umount loop - Compress it: gzip -9 rootfs Making a SYSLINUX boot floppy ----------------------------- The following steps will create the boot floppy. Note: You will need to have the mtools package installed beforehand. - Insert a floppy in the drive and format it with an MSDOS filesystem: mformat a: (if the system doesn't know what device 'a:' is, look at /etc/mtools.conf) - Run syslinux on the floppy: syslinux -s /dev/fd0 (the -s stands for "safe, slow, and stupid" and should work better with buggy BIOSes; it can be omitted) - Put on a syslinux.cfg file: mcopy syslinux.cfg a: (more on syslinux.cfg below) - Copy the root file system you made onto the MSDOS formatted floppy mcopy rootfs.gz a: - Build a linux kernel and copy it onto the disk with the filename 'linux' mcopy bzImage a:linux Sample syslinux.cfg ~~~~~~~~~~~~~~~~~~~ The following simple syslinux.cfg file should work. You can tweak it if you like. ----begin-syslinux.cfg--------------- DEFAULT linux APPEND initrd=rootfs.gz root=/dev/ram0 TIMEOUT 10 PROMPT 1 ----end-syslinux.cfg--------------- Some changes you could make to syslinux.cfg: - This value is the number seconds it will wait before booting. You can set the timeout to 0 (or omit) to boot instantly, or you can set it as high as 10 to wait awhile. - PROMPT can be set to 0 to disable the 'boot:' prompt. - you can add this line to display the contents of a file as a welcome message: DISPLAY display.txt Additional Resources -------------------- Other useful information on making a Linux bootfloppy is available at the following URLs: http://www.linuxdoc.org/HOWTO/Bootdisk-HOWTO/index.html http://www.linux-embedded.com/howto/Embedded-Linux-Howto.html http://linux-embedded.org/howto/LFS-HOWTO.html http://linux-embedded.org/pmhowto.html http://recycle.lbl.gov/~ldoolitt/embedded/ (Larry Doolittle's stuff) Possible TODOs -------------- The following features that we might want to add later: - support for additional filesystems besides ext2, i.e. minix - different libc, static vs dynamic loading - maybe using an alternate bootloader
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值