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

时间:2022-09-22 03:49:30
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,具体见