11月24日 layouts and rendering in rails(部分没有看)
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(部分没有看)的更多相关文章
- 2016年11月24日 星期四 --出埃及记 Exodus 20:15
2016年11月24日 星期四 --出埃及记 Exodus 20:15 "You shall not steal.不可偷盗.
- 键盘和鼠标闲置超时时关闭显示器并锁定电脑桌面的AutoHotkey脚本 2019年11月24日写
/* 键盘和鼠标闲置超时时关闭显示器并锁定电脑桌面的AutoHotkey脚本 2019年11月24日写 在电脑桌面锁定时移动鼠标就会显示登录界面,此时即使超过电源设置的时间电脑也不会关闭显示器使得屏幕 ...
- 2016年11月24日--面向对象、C#小复习
面对对象就是:把数据及对数据的操作方法放在一起,作为一个相互依存的整体——对象.对同类对象抽象出其共性,形成类.类中的大多数数据,只能用本类的方法进行处理.类通过一个简单的外部接口与外界发生关系,对象 ...
- 2016年11月8日 星期二 --出埃及记 Exodus 19:24
2016年11月8日 星期二 --出埃及记 Exodus 19:24 The LORD replied, "Go down and bring Aaron up with you. But ...
- SQL PASS将于8月24日在北京中医药大学举办线下活动
活动主题:复制架构的实现和调优以及SQL Server BI在传统行业的应用 地点:北三环东路11号 北京中医药大学 白色的1号楼教学楼后楼5层511房间 时间:2013年8月24日 9:00-12: ...
- 6月24日AppCan移动开发者大会礼品清单遭泄露
6月24日,第一届AppCan移动开发者大会将在北京国际会议中心举办,大会以”平台之上,应用无限”为主题,全景展现移动应用发展趋势.AppCan 移动技术蓝图及80万开发者的技术实践成果. 大会现场礼 ...
- 全国Uber优步司机奖励政策 (1月18日-1月24日)
本周已经公开奖励整的城市有:北 京.成 都.重 庆.上 海.深 圳.长 沙.佛 山.广 州.苏 州.杭 州.南 京.宁 波.青 岛.天 津.西 安.武 汉.厦 门,可按CTRL+F,搜城市名快速查找. ...
- 9月24日开始发布,主打安全的Librem 5 Linux手机
曾推出搭载PureOS Linux发行版本Librem笔记本系列的硬件厂商Purism,今天正式宣布了Librem 5 Linux手机的最终和官方发售日期.Librem 5于2017年10月正式发布, ...
- 36.React基础介绍——2019年12月24日
2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...
随机推荐
- git初学
git在团队合作开发时是很有用的,SVN是集中式的代表,而git是分布式的代表,它分为代码区.暂存区.和本地库.在同一个团队中开发时,在代码存储中心(例如,码云.github)上创建一个库,用于储存和 ...
- Bootstrap3基础 img-rounded 图片的四个角改成圆角
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- dart实例
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...
- 题解——洛谷P3390 【模板】矩阵快速幂(矩阵乘法)
模板题 留个档 #include <cstdio> #include <algorithm> #include <cstring> #define int long ...
- MySQL的启动和关闭
1.Windows下 启动服务 mysqld --console 或 net start mysql 关闭服务 mysqladmin -uroot shudown 或 net stop mysql 2 ...
- Google advertiser 开发
开发 相关知识: 单一归因与多重归因 当展示广告网络上发生一次展示时,您可按单一归因或多重归因记录对展示起到影响作用的条件. 单一归因 在使用单一归因时,系统只为指定的展示记录一个触发条件(如展示位置 ...
- Images之Dockerfile中的命令2
COPY COPY has two forms: COPY [--chown=<user>:<group>] <src>... <dest> COPY ...
- Kubernetes相关概念
This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can exp ...
- SMTP发送邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...
- 启动总是提示:Process finished with exit code 0
1.端口冲突检查端口号 2.缺少web启动依赖 <dependency> <groupId>org.springframework.boot</groupId> & ...