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 up your 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 that the 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,具体见

11月24日 layouts and rendering in rails(部分没有看)的更多相关文章

  1. 2016年11月24日 星期四 --出埃及记 Exodus 20:15

    2016年11月24日 星期四 --出埃及记 Exodus 20:15 "You shall not steal.不可偷盗.

  2. 键盘和鼠标闲置超时时关闭显示器并锁定电脑桌面的AutoHotkey脚本 2019年11月24日写

    /* 键盘和鼠标闲置超时时关闭显示器并锁定电脑桌面的AutoHotkey脚本 2019年11月24日写 在电脑桌面锁定时移动鼠标就会显示登录界面,此时即使超过电源设置的时间电脑也不会关闭显示器使得屏幕 ...

  3. 2016年11月24日--面向对象、C#小复习

    面对对象就是:把数据及对数据的操作方法放在一起,作为一个相互依存的整体——对象.对同类对象抽象出其共性,形成类.类中的大多数数据,只能用本类的方法进行处理.类通过一个简单的外部接口与外界发生关系,对象 ...

  4. 2016年11月8日 星期二 --出埃及记 Exodus 19:24

    2016年11月8日 星期二 --出埃及记 Exodus 19:24 The LORD replied, "Go down and bring Aaron up with you. But ...

  5. SQL PASS将于8月24日在北京中医药大学举办线下活动

    活动主题:复制架构的实现和调优以及SQL Server BI在传统行业的应用 地点:北三环东路11号 北京中医药大学 白色的1号楼教学楼后楼5层511房间 时间:2013年8月24日 9:00-12: ...

  6. 6月24日AppCan移动开发者大会礼品清单遭泄露

    6月24日,第一届AppCan移动开发者大会将在北京国际会议中心举办,大会以”平台之上,应用无限”为主题,全景展现移动应用发展趋势.AppCan 移动技术蓝图及80万开发者的技术实践成果. 大会现场礼 ...

  7. 全国Uber优步司机奖励政策 (1月18日-1月24日)

    本周已经公开奖励整的城市有:北 京.成 都.重 庆.上 海.深 圳.长 沙.佛 山.广 州.苏 州.杭 州.南 京.宁 波.青 岛.天 津.西 安.武 汉.厦 门,可按CTRL+F,搜城市名快速查找. ...

  8. 9月24日开始发布,主打安全的Librem 5 Linux手机

    曾推出搭载PureOS Linux发行版本Librem笔记本系列的硬件厂商Purism,今天正式宣布了Librem 5 Linux手机的最终和官方发售日期.Librem 5于2017年10月正式发布, ...

  9. 36.React基础介绍——2019年12月24日

    2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...

随机推荐

  1. Oracle常用函数——TO_DATE

    TO_DATE 含义:将具有固定格式的字符串类型的数据转化为相对应的Date类型数据,官网解释如下图   使用方法 TO_DATE("需要转换的字符串","日期格式&qu ...

  2. OCX ClassId查看

    1.源码中查看 2.注册完成后在注册表中查看 a.windows键+R  打开“运行” b.输入“regedit”  ,回车 c. d.再输入框中输入ocx名称,点击查找下一个 e.展开,点击CLSI ...

  3. javaScript随机数取值方法

    Math.random()方法返回0到1之间的一个随机数,不包括0和1 如若想取的一个范围的随机数可套用下面的公式: 一.X+开始数-1=结束数 二.Math.floor(Math.random()* ...

  4. MySQL 5.7的原生JSON数据类型使用

    新增测试用表: CREATE TABLE lnmp ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `category` JSON, `tags` JSON, P ...

  5. P1829 [国家集训队]Crash的数字表格 / JZPTAB

    推式子太快乐啦!虽然我好蠢而且dummy和maomao好巨(划掉) 思路 莫比乌斯反演的题目 首先这题有\(O(\sqrt n)\)的做法但是我没写咕咕咕 然后就是爆推一波式子 \[ \sum_{i= ...

  6. js实现类型jq的dom加载完成

    有时候我们只想在 dom 加载完成后运行 js ,而不是等所有图片加载完成.所以不需要 onload , onload 会加载图片等其他媒体.很消耗时间. 原:http://blog.csdn.net ...

  7. 51nod 1437 迈克步(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...

  8. React内三种函数的写法

     以下提供三种React内函数的写法,都可以正常运行,有疑问可以留言 写法一:让函数内部的this指向这个类的实例,它是用bind实现的,bind的第一个参数表示context,就是this. //写 ...

  9. jmeter命令行模式运行,实时获取压测结果

    jmeter命令行模式运行,实时获取压测结果 jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script. ...

  10. <aop:aspect>与<aop:advisor>的区别

    在开发过程中,不少有Spring Aop的使用,在面向切面编程时,我们会使用< aop:aspect>:在进行事务管理时,我们会使用< aop:advisor>.那么,对于&l ...