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

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 方法都能担起重则,负责渲染应用的内容,供浏览器使用。render方法可以customize客制化

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

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

3.render 应用之外的视图: 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,普通的javascript.



2.2 render 方法的选择:5个

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

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

render layout: "special_layout"

:location   //不太明白,以后再会看。

: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: :forbidden

:formats  //



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 unnamed yield. To render content into a named yield, you use the content_for method.

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 up the 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"} %>



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 using nested layouts(sometimes called sub-templates).

时间: 2024-07-28 17:42:56

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

新路由3(newifi d2)OpenWrt R8.1.1 By Lean 11月24日编译 持续更新

源码:https://github.com/coolsnowwolf/lede 环境:debian 9.6 (2018年11月4日之前deepin 15.7 deepin除了好看就是......) 登陆 ip 192.168.1.1 密码 password 链接: https://pan.baidu.com/s/1eSzQr3FNCdbA6i0EfnfT7g 提取码: t7pe 日常更新主要以楼主的需求 加入的详细组件在分享文件夹里的manifest文件里有详细的记录 另外应某些朋友的要求  加

不能容忍的开放式婚姻2014年11月24日

柠檬系列博客 当朋友报告我,她离婚了,我真的不信,感受他们小妇妻俩看起来是多么的恩爱苦蜜.追问本果,她说了:我真在是不克不及容忍他推许的开放式婚姻. 妇妻两边真行的对待性死活方里所收受接收的随便死活方式,即性死活随便,彼此不束厄局促.这类婚姻类似于男女跳舞,感受美好则接着跳,感受短好则各奔器械,各找各的.在确定较固定的舞陪之前,仄日战若干人跳过,不真践何处切身感受?不相比若何判别谁最好?这类人日常不把匹配看得太重,更不视为一成不变,既然能匹配,若感应不荣幸,虽然就可以够离婚. 人道的解放是社会死

【心情】11月24日

今天天气真冷....话说越接近冬天就越不想起床. 补了一周的学习,终于将拉下的一个半月的学习以质量不高的方式补完了,搞得我一个星期都没往机房跑. 这周又可以开始上竞赛了,同桌不知道怎么搞得突然变得特别热爱竞赛. 感觉周计划还是蛮有效果的,合理制定自己的计划确实对提高学习效率有很大的提高(还是被一学霸教导的=_=). 有时间会把省选范围内的知识点稍微总结一下,但在开始省选之前还是先打好基础吧. 又弄到了一本数学书,不知道什么时候才刷的完啊,还有先前几本书还没有刷完的!感觉下一次月考又要跪烂了...

SQL server while循环使用,存储过程 11月24日

今天首先学习了SQL server中循环语句while的使用 之后学习了case  when 的使用,when需要跟then  配合使用 2.SQL server中存储过程的使用情况及举例 存储过程练习: 1.输入一个数,求1~n的和

11月24日

彻底明白一致质量矩阵.刚度矩阵的计算方法 明白了中心差商求解器的计算流程 下一步是要了解降维积分的做法 来自为知笔记(Wiz)

20171124_Python学习六周五次课(11月24日)

今日任务: 13.1 NoSQL简介13.2 redis服务搭建13.3 redis连接池13.4 redis管道 笔记: 1.      Python操作nosql数据库 NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展.NoSQL数据库的产生就是为了解决大规模数据集合多重数据种

5月24日-----疯狂猜成语-----四周第二次站立会议 参会人员:杨霏,袁雪,胡潇丹,郭林林,尹亚男,赵静娜

疯狂猜成语-----四周第六次站立会议 参会人员:杨霏,袁雪,胡潇丹,郭林林,尹亚男,赵静娜 会议内容:在寻找用户过程中,记录总结用户建议和意见. 用户一: 用户姓名 唐仙迪 联系电话 18330106893 所用时间 12分钟 用户使用过程中出现的问题 无 用户评价 整体很好,界面做的比较不错 用户对产品提出的意见 成语数量增加点 自己对意见的解决办法  增加数据库的存储量 用户二: 用户姓名 司新红 联系电话 18330107807 所用时间 9分钟 用户使用过程中出现的问题 无 用户评价

第19期中国智能家居主题沙龙将于9月24日在上海蓝天经济城举行

全民健康意识提升,健康型智能硬件成爆品,智能家居主打健康功能将大有可为.随着2013年下半年智能空气净化器突然走俏,人们由空气健康扩展为对整个家庭健康问题的普遍关切,一个极具行业前景的隐性市场正迎来光明. 背景:   在智能家居主推的智能.便捷的等功能需求的情况下,健康作为家庭用户的潜在需求尚未完全被认可.随着2013年下半年智能空气净化器突然走俏,人们由空气健康扩展为对整个家庭健康问题的普遍关切,一个极具行业前景的隐性市场正迎来光明.   进入2014年,个体健康领域并未从智能家居爆发,而是首

第四期RHCA 限时半价 8月24日开课

不知不觉间老段工作室即将成立3周年了,依靠广大童鞋的支持,3年间培训了近600名RHCE,近40名RHCA,近200名OCP,其他课程学员近500名(以上数据为笼统统计).老段工作室同时也共享了大量的视频以供同学们学习及了解上课情况. 为了感谢各位童鞋们的支持,此次3周年举行推出"第四期RHCA培训特大优惠"活动,新学员报名培训,价格只要¥6500,老学员报名优惠价¥6000. 报名本地培训,如果参加考试的童鞋,每门赠送一门补考机会! 前十名报名更可获取"老段带你学mysql