http://guides.rubyonrails.org/layouts_and_rendering.html 

中文

This guide covers the basic layout features of Action Controller and Action View

After reading the guide, i will know:

1.How to use the various rendering methods bulit into Rails.

2.How to create layouts with multiple content sections.(如何创建出具有多个内容区域的布局)

3.How to use partials to Dry upyour views. (如何弄干你的可视页面,优化得更简洁清晰)

4.How to use nested layouts (sub-templates)


一 overview:How the Pieces fit together

model-controller-view triangle,mvc三角,通过controller 让model干重活,让view干response响应用户的工作,如页面布局。本章讲的就是controller与views的互动设计interaction design.

总体说,当收到用户request,如何根据request,调用method来创建相应的response ,并送回信息过去。如果response是一个完整的view,rails需要做layout ,包括partial view的嵌套。

二,Creating Responses :three ways to create an HTTP response

1. render ;

2  redirect_to;

3  head


2.1 Rendering by Default: convention over configration in Action(多约定,少配置)

原则:convention over configration principle

controller根据routes来自动调用view,然后render渲染。

真正处理渲染的过程是 ActionView::TemplateHandlers 的子类。

This guide does not dig into that process, but it's important to know thatthe file extension on your view(视图文件扩展名)controls the choice of template handler. 现在用的是.erb(HTML with embedded Ruby)

2.2 Using render.

ActionController::Base#render方法能担起重则,负责渲染应用的内容content供浏览器使用。render方法可以customize客制化:

1.render同一个controller中的其他template: render "name"

2.render其他ccontroller的某个template:render "products/name" //早期版本render template: "p/n"

3.render 应用之外的视图file: render file:"u/apps/warehouse_app/..."需要绝对路径。微软系统值支持这种绝对路径。

4.render文本:render plain: "ok任意文本" (渲染纯文本主要用于响应 Ajax 或无需使用 HTML 的网络服务

5.render HTML:You can send an html_string back to the browser by using the :html option to render

例子render html: "<strong>Not Found</strong>".html_safe

用途:只渲染一个小html片段。

6.Render JSON: render json: @product(JSON 是一种 JavaScript 数据格式,很多 Ajax 库都用这种格式。Rails 内建支持把对象转换成 JSON,经渲染后再发送给浏览器

7.还可以渲染xml

8.渲染Vanilla JavaScript。原生javascript. 发送string到浏览器,格式是text/javascript.

render js: 'alert("Hello Rails"); '


2.2.12 render 方法的选择options

1. :content_type  //用于修改返回的渲染结果的格式类型,默认render返回的是text/html格式,可以设置格式为 application/json或者application/xml.

render file: filename, content_type: 'application/rss'

2. :layout  //可以告诉rails,在当前动作中使用指定的文件作为布局,详细的见后面。

render layout: "special_layout"

3. :location   //用于设置HTTP Location header。

4. :status   //Rails will automatically generate a response with the correct HTTP status code. You can use the :status option to change this;

render status: 500  或者 render status: :internal_server_error

render status: :forbidden  就是403

5. :formats  //指定了request中的format。找对应的模版,找不到则Action::MissingTemplate升起来。

render formats: :json

2.2.13Finding Layouts

Rails 首先找app/views/layouts 内与controller同名的文件,如果没有则使用默认的application.html.erb or .builder.

Rails 提供了多种方法指定individual controllers and actions.

1.Specifying Layouts for Controllers: layout "name"

2.没细看:可以客制化布局,比如根据用户的选择,来使用特殊的布局渲染产品视图。见原文2.2.13.2

3.根据 条件设定布局: :only和:except选项,限制用到的方法。

例子:

class ProductsController<ApplicationController
  layout "product", except: [:index, :rss]
end

4 布局可以继承。layout inheritance.如果在contoller中指定布局,则优先用指定布局。

⚠️ :不要在一个action中双重渲染:Can only render or redirect once per action(一个动作只能渲染或重定向一次)


2 Using redirect_to

发起新的request,执行新的代码,些经验不足的开发者会认为redirect_to方法是一种goto命令,把代码从一处转到别处。这么理解是不对的。执行到redirect_to方法时,代码会停止运行,等待浏览器发起新请求。你需要告诉浏览器下一个请求是什么,并返回 302 状态码。

这个代码不适合大型应用,会增加响应的时间,影响效能。


3 Structuring Layouts 组织页面的布

三种工具:

1.asset tag

2.yield and content_for(设定指定的yield的时候,需要用content_for,具体见?例子)

3.partial 局部视图


Yield:with the context of a layout, yield identifies a section where content from the view should be inserted.

pasting

<html>
  <head>
  <%= yield :head %> //布局可以标明多个区域
  </head>
  <body>
  <%= yield %>
  </body>
</html>

The main body of the view will always render into the unnamedyield. To render content into a namedyield, you use thecontent_formethod.

For example,this view would work the layout that you just saw:

<% content_for :head do %>
  <title>A simple page</title>
<% end %>
<p>Hello, Rails!</p>

The result of rendering this page into the supplied layout would be this HTML:

<html>
  <head>
  <title>A simple page</title>
  </head>
  <body>
  <p>Hello, Rails!</p>
  </body>
</html>

侧边栏,页脚都可以用这个content_for 方法。


Partials:partial templates

在views中渲染局部view可以render 方法

例子:<%= render "shared/menu" %>

Using Partials to Simplify Views:dry upthe view 简化视图。

<%= render "shared/ad_banner" %>
<h1>Products</h1>
<p>Here are a few of our fine products:</p>
...
<%= render "shared/footer" %>

pasting


3.4.3

Partial layouts:

A partial can use its own layout file, just as a view can use a layout. For example:

<%= render partial: "link_area", layout: "graybar" %>

这行code将寻找文件_link_area.html.erb,并且使用layout文件_graybar.html.erb渲染(render)它.layout文件也有下划线并在partial的同一folder中,而不是在the master layouts folder中。


3.44

Passing Local Variables


3.45

Rendering Collections 渲染集合,使用局部视图特别方便。

全栈课程里 有用过。使用:collection选项。例子:

<%= render partial: "product", collection:"@products" %>

handshort方式:<%= render @products %>

Rails根据集合中的各个元素的model的名字来使用partial的名字。 集合中的元素可以来自不同的模型,Rails会选择正确的局部视图进行渲染。

:as可以自定义局部变量的名字用在partial中。

:locals:{}选项可以把任意局部变量传人局部view

<%= render partial: "product", collection: @products,
           as: :item, locals: {title: "Products Page"} %>

 解释loacals:{title:"Products Page"}//把字符串Products Page传入局部view,由title接收这个字符串。


3.48

Collection Partial Layouts

见上面Partial layouts.

<%= render partial: "product", collection: @products, layout: "special_layout" %>

pasting


3.5 nested Layout

You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller.Rather than repeating the main layout and editing it, you can accomplish(努力完成) this by usingnested layouts(sometimes called sub-templates).

转载于:https://www.cnblogs.com/chentianwei/p/7899329.html

11月24日 layouts and rendering in rails(部分没有看)相关推荐

  1. 如何利用大数据做金融风控? 原创 2016年11月24日 17:42:03 标签: 大数据 / 金融 / 风控 1594 导语:如何通过海量数据与欺诈风险进行博弈? 随着金融科技、科技金融等概念的

    如何利用大数据做金融风控? 原创 2016年11月24日 17:42:03 标签: 大数据 / 金融 / 风控 1594 导语:如何通过海量数据与欺诈风险进行博弈? 随着金融科技.科技金融等概念的热起 ...

  2. Linux 内核 5.4 将于 11月24 日 发布,Linux 5.4-rc8 已可用于公测

    Linux 内核5.4 将计划于2019年11月24日发布.而在上周末,Linus Torvalds则宣布了最后一个发布候选版本(RC). Linus Torvalds在邮件列表公告中表示," ...

  3. 【历史上的今天】11 月 24 日:美国在线收购网景;太阳能光伏之父出生;HTML 2.0 发布

    整理 | 王启隆 透过「历史上的今天」,从过去看未来,从现在亦可以改变未来. 今天是 2022 年 11 月 24 日,在 1859 年的今天,达尔文<物种起源>第一版发行,共 1250 ...

  4. 【财经期刊FM-Radio|2020年11月24日】

    title: [财经期刊FM-Radio|2020年11月24日] 微信公众号: 张良信息咨询服务工作室 [今日热点新闻一览↓↓] 美股走出一周低谷,布油创八个月新高,金银大跌,苹果跌近3%,特斯拉和 ...

  5. 11月24日杭州大数据技术沙龙来袭,网易大数据、网易严选、蚂蚁金服大咖亲身经验分享!

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 从"互联网+.移动互联网.AI+"等概念的更迭,大家对数据的认知越来越清晰.数据既是线上场景的产物,反之又作用线上场景的持续优化 ...

  6. 九阴服务器延迟高怎么解决,九阴真经 11月24日合服公告

    亲爱的玩家: 为了让您享受更优质的服务和更好的游戏环境,<九阴真经>运营团队决定于11月24日对部分服务器进行数据互通升级,同时通过技术优化,进一步提高服务器承载上限,以求让更多的玩家可以 ...

  7. 11月24日struts培训日记

    首先为大家分析了struts框架的工作原理和体系结构,讲解了ActionServlet.Action.ActionForm.ActionForward等类的作用与工作关系.struts-config. ...

  8. 个人站立会议(11月24日)

    今天所做:查找窗口设计的资料,练习并熟悉掌握关于窗口设计的代码编写. 遇到问题:查找资料,没有方向. 计划明天:修复bug,并尝试添加新的内容. 转载于:https://www.cnblogs.com ...

  9. 11月24日学习笔记_map/reduct的应用于使用

    首先我们先了解几个命令的含义: print(str.upper()) # 把所有字符中的小写字母转换成大写字母 # print(str.lower()) # 把所有字符中的大写字母转换成小写字母 # ...

最新文章

  1. java 0x转中文_Java:转换汉字为unicode形式的字符串和转换unicode形式字符串转换成汉字...
  2. 【car】购买新能源电动汽车的几个注意事项
  3. ASP.NET .Net UCS2 加码最复杂的方法
  4. Java技巧:创建监视友好的ExecutorService
  5. python如何运行py程序_如何用Python汇款:Web3.py教程
  6. 析构函数virtual与非virtual区别
  7. Github:视觉问答最新资源汇总
  8. Android系统(134)--- Android关于OOM的解决方案 ##OOM
  9. DevExpress GridControl 导出为Excel
  10. 一文读懂数据平台、大数据平台、数据中台
  11. python调用matlab绘图_python初步调用MATLAB及网址存档
  12. [Flink]Flink 的物理分区器
  13. Cocos2d JS 之消灭星星(十) 关卡配置
  14. 3992. 树上有猴-AcWing题库
  15. C/C++ Npcap包实现ARP欺骗
  16. 工业互联网大数据平台建设方案
  17. 高数_第3章重积分_三重积分可证明为3个定积分的乘积__很重要
  18. 微信支付和支付宝支付整合(含设计模式1)
  19. linux如何给脚本等创建一个桌面启动图标
  20. 快手大数据平台服务化实践

热门文章

  1. 阿里云智能数据构建与管理 Dataphin公测,助力企业数据中台建设
  2. cocos2d-x3.2对CocoStudio的支持
  3. SQL Server删除整个数据库中表数据的方法(不影响表结构)
  4. Exception in thread main java.lang.NoClassDefFoundError
  5. Android开发之发送短信
  6. KlayGE 4.4中渲染的改进(五):OpenGL 4.4和OpenGLES 3
  7. 手抄Threejs源码之Scene
  8. emacs 编辑模式_作家的5种Emacs模式
  9. javacv入门指南:序章_建立开放文化的循序渐进指南
  10. 55种数据可视化开源工具_4种开源工具让我的创业起步