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. python --- 18 类与类之间的关系, 特殊成员

    类与类之间的关系 一.依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作      随时可以更换另外一个东西来完成此操作 此时的关系是最轻的. 二.关联关系(组合关系,聚合关系) 1.一对 ...

  2. FireMonkey 源码学习(5)

    (5)UpdateCharRec 该函数的源码分析如下: procedure TTextLayoutNG.UpdateCharRec(const ACanvas: TCanvas; NeedBitma ...

  3. Python3基础 list range+for 等差数列

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. dart基础语法

    .关于 runApp() 上面的实例代码中使用了 runApp() 方法,runApp 方法接收的指定参数类型为 Widget,即: runApp(Widget).在 Flutter 的组件树(wid ...

  5. extjs的使用笔记2

    系统的大部分资源(安装程序的除bin, lib, conf等之外的东西)都是放在 /usr/share/目录中的 在用户自己定义的, 一些关于系统资源的东西, 则放在目录 ~/.local/share ...

  6. 三星sm865

    目录 样张 SSD-Z: CrystalDiskInfo: CrystalDiskMark: AS-SSD Benchmark: 颗粒检查: 扇区信息: HD Tune Pro: 三星Magician ...

  7. CssClass="Hidden"和Visible="False"

    <asp:Label ID="lblNoCustomTableItemCheckedInfo" runat="server" CssClass=" ...

  8. Derek解读Bytom源码-P2P网络 upnp端口映射

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  9. 【C#】神奇的yeild

    直接出栗子: class Program { static void Main(string[] args) { foreach (var item in FilterWithoutYield) { ...

  10. C盘清理

    魔方清理大师 “清理大师”->清理界面 一键清理=>开始扫描=>立刻清理 注册表清理=>开始扫描=>立刻清理 深度清理=>开始扫描=>立刻清理 C:\User ...