---
title: "ruby on rails（7）-- 创建一个页面模版"
author: "black_star"
date: "2009-11-26T21:54:17Z"
tags: ["CSS", "HTML", "Rails", "Ruby", "XHTML"]
slug: "ruby-on-rails-7"
draft: false
lastmod: "2009-11-26T21:55:12Z"
id: 528632
url: "https://black-star.iteye.com/blog/528632"
---

创建一个页面模版，你可以很轻松的往里面填内容，而如果改动样式时，只要改动一个文件即可。rails 会在app/view/layout下面根据controller默认选中名字一样的layout。而我们的controller为store，因此rails会默认选中layout下的store.html.erb文件。因此，最简单的就是手动创建store.html.erb。这样，就能默认载入了。 \[code="java"] Pragprog Books Online Store &lt;%= stylesheet\_link\_tag "depot",:media =&gt; "all" %&gt;

&lt;%=image\_tag("logo.png") %&gt; &lt;%=@page\_title || 'Pragmatic Bookself'%&gt;

[Home](#)  
[Questions](#)  
[News](#)  
[About](#)

&lt;%=yield :layout %&gt;

&gt; \[/code] &lt;%=image\_tag("logo.png") %&gt;导入logo图片 &lt;%=@page\_title || 'Pragmatic Bookself'%&gt;会在store\_controller里面寻找@page\_title 如果有且可以访问则输出它的值,否则 用后面取代 &lt;%=yield :layout %&gt; &lt;%=yield%&gt;相当于一个宏，:layout会默认选中和contoller名字一样的view文件夹下的index.html.erb。这里我们的controller为store。因此是view/store/index.html.erb/ 关于:layout的用法，共有5种layout .http://www.iteye.com/topic/106623详解 然后编辑css。 还有一个是对价格格式化输出number\_to\_currency(product.price) 11.1 -&gt; $11.1 12344 -&gt; $123,45 图片弄好后，就是需要放到购物车里面了。此时需要一个连接。基本是一个连接图片，或者一个Button。 普通[产生的链接是采取get方式。基本上是对信息的取，没有交互。而rails里面button\_to 包含一个form ,采取post形式，可以交互。因此采用button\_to。一下是view/store/index.html.erb \[code="java"\] **Store#index**  
\
Find me in app/views/store/index.html.erb  
\
&lt;% for product in @products -%&gt; &lt;%= image\_tag(product.image\_url) %&gt; **&lt;%=h product.title %&gt;**  
&lt;%= product.description %&gt; &lt;%= number\_to\_currency(product.price)%&gt; &lt;%=button\_to 'Add to Cart',:action =&gt; :add\_to\_action,:id =&gt; product %&gt; &lt;% end %&gt; \[/code\] :action =&gt; :add\_to\_action action为定义 :id主要是为了区别不同的商品，以后使用 \[color=red\]网上搜东西的时候，搜到一个rails得视频网站，推荐http://railscasts.com/\[/color\]]()