ITeye博客

ruby on rails(7)-- 创建一个页面模版

black_star · 2009-11-26 21:54 · ruby on rails · HTML · CSS · Ruby · Rails · XHTML · Markdown

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

<%=image_tag("logo.png") %> <%=@page_title || 'Pragmatic Bookself'%>

Home
Questions
News
About

<%=yield :layout %>

> [/code] <%=image_tag("logo.png") %>导入logo图片 <%=@page_title || 'Pragmatic Bookself'%>会在store_controller里面寻找@page_title 如果有且可以访问则输出它的值,否则 用后面取代 <%=yield :layout %> <%=yield%>相当于一个宏,: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 -> $11.1 12344 -> $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

<% for product in @products -%> <%= image_tag(product.image_url) %> <%=h product.title %>
<%= product.description %> <%= number_to_currency(product.price)%> <%=button_to 'Add to Cart',:action => :add_to_action,:id => product %> <% end %> [/code] :action => :add_to_action action为定义 :id主要是为了区别不同的商品,以后使用 [color=red]网上搜东西的时候,搜到一个rails得视频网站,推荐http://railscasts.com/[/color]

评论(2)

  1. #2 楼 · black_star · 2009-11-26 22:49

    呵呵,笨鸟多飞飞

  2. #1 楼 · supercode · 2009-11-26 22:25

    楼主真勤奋

评论功能维护中,当前仅展示历史评论。