`

Rendering in Rails

阅读更多
参考链接:http://guides.rubyonrails.org/layouts_and_rendering.html

render

如果action中没有调用render、redirect_to、head和method_missing方法中的任何一个,rails默认会去渲染和当前action名字相对应的那个模板。比如一个BooksController里有这样一段代码:
def show
  @book = Book.find(params[:id])
end

rails将会在执行完show方法后渲染 app/views/books/show.html.erb 模板。

引用
If you want to see the exact results of a call to render without needing to inspect it in a browser, you can call render_to_string. This method takes exactly the same options as render, but it returns a string instead of sending a response back to the browser.


如果你希望给浏览器发送一个空的响应信息,可以使用:nothing选项
render :nothing=>true

render的第一个参数如果是一个string或者symbol,rails则会去渲染同一个controller下的某action对应的模板,比如:
def update
 @book = Book.find(params[:id])
  if @book.update_attributes(params[:book])
   redirect_to(@book)
  else
   render "edit" #OR render :edit
  end
 end
end 

上面的代码在@book.update_attributes方法返回false时,将会去渲染这个controller中edit这个action对应的模板。下面的代码有同样的效果,只是在Rails2.3中没必要写得这么麻烦:
def update
 @book = Book.find(params[:id])
  if @book.update_attributes(params[:book])
   redirect_to(@book)
  else
   render :action=>'edit'
  end
 end
end 

如果要渲染的是另一个controller的模板,可以在render方法的参数中直接指定那个模板的路径(相对于app/views的相对路径),比如说你要在app/controllers/admin下的AdminProductsController中渲染app/views/products/show.html.erb模板,可以这样调用render:
render 'products/show'

下面的代码有同样的效果,只是在Rails2.3中没必要写得这么麻烦:
render :template => 'products/show' 

还可以渲染其它任意位置的模板文件,比如说在某种情况下,两个rails应用共享了同一个模板文件:
render "/u/apps/warehouse_app/current/app/views/products/show"

rails根据参数的第一个字符是否"/"来判断这是不是一个file render(怎么翻译? - -)。
下面的代码有同样的效果,只是在Rails2.3中没必要写得这么麻烦:
render :file => "/u/apps/warehouse_app/current/app/views/products/show"

默认的,file render不使用当前的layout,如果你打算让它使用当前的layout,必须指定:layout=>true。

注意:如果你的rails运行在Microsoft Windows上,则必须使用:file选项来渲染文件,因为Windows文件名的格式和Unix不一样。

:inline选项可以让render方法渲染一个字符串,而不是一个erb模板:
render :inline => "<% products.each do |p| %><p><%= p.name %><p><% end %>" 

几乎没有什么理由使用:inline这个选项,在controller逻辑中混入ERB代码违反了Rails的MVC原则并且使其它开发者难以理解你的程序逻辑。最好是用分离了"逻辑"和"表现"的erb模板来代替:inline选项。
inline rendering默认使用ERB,你也可以通过指定:type选项来强制它使用Builder:
render :inline => "xml.p {'Horrid coding practice!'}", :type => :builder

引用
Using render with :update

You can also render javascript-based page updates inline using the :update option to render:
render :update do |page|
 page.replace_html 'warning', "Invalid options supplied" 
end


Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. We recommend using a separate rjs template instead, no matter how small the update.

通过:text选项可以把一个纯文本内容发送给浏览器:
render :text =>'hello'

引用
Rendering pure text is most useful when you’re responding to AJAX or web service requests that are expecting something other than proper HTML.

和file render一样,默认的,:text选项不使用当前的layout,如果你打算让它使用当前的layout,必须指定:layout=>true。

渲染json
很简单:
render :json=>@product

引用
You don’t need to call to_json on the object that you want to render. If you use the :json option, render will automatically call to_json for you.

渲染xml
同json:
render :xml=>@product
后面的那小段英文就不多引用了,一样。
Rendering Vanilla JavaScript
render :js => "alert('Hello Rails');"

Options for render
render方法还接受4个选项:
:content_type
:layout
:status
:location
:content_type
引用
By default, Rails will serve the results of a rendering operation with the MIME content-type of text/html (or application/json if you use the :json option, or application/xml for the : xml option.). There are times when you might like to change this, and you can do so by setting the :content_type option:
render :file => filename, :content_type => 'application/rss'

:layout
引用
You can use the :layout option to tell Rails to use a specific file as the layout for the current action:
render :layout => 'special_layout'

You can also tell Rails to render with no layout at all:
render :layout => false 

:status
引用
Rails will automatically generate a response with the correct HTML status code (in most cases, this is 200 OK). You can use the :status option to change this:
render :status => 500 render :status => :forbidden

Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in actionpack/lib/action_controller/status_codes.rb. You can also see there how Rails maps symbols to status codes.

:location
引用
You can use the :location option to set the HTTP Location header:
render :xml => photo, :location => photo_url(photo)
分享到:
评论

相关推荐

    Practical Physically Based Rendering in Real-time

    Practical Physically Based Rendering in Real-time

    Advanced Real Time Rendering in 3D Graphics and Games.pdf

    《Advanced Real Time Rendering in 3D Graphics and Games》是一本专为游戏开发和实时渲染技术设计的专业书籍。这本书深入探讨了3D图形学的核心概念,特别是如何在游戏环境中实现高效且高质量的实时渲染。实时渲染...

    #GDC2017 FrameGraph: Extensible Rendering Architecture in Frostbite

    “FrameGraph: Extensible Rendering Architecture in Frostbite” – Yuriy O’Donnell (Frostbite / Electronic Arts)

    Advanced Real-Time Rendering in 3D Graphics and Games

    ### 高级实时渲染在3D图形与游戏中的应用 #### 核心概念与背景介绍 随着技术的进步和GPU计算能力的不断增强,高级实时渲染技术已成为3D图形和游戏开发领域的重要研究方向之一。该技术旨在提高虚拟世界的交互性和...

    Volume Rendering in Medical Applications

    体积渲染技术在医学应用中,主要指的是利用三维图像合成技术将医学扫描设备获取的二维图像切片转化为三维图像。该技术通过图像渲染算法,结合照明、分类、合成方案等必要成分,生成可视化的三维图像,帮助医生和放射...

    Advances in Real Time Rendering Course

    ### 实时渲染技术进展课程概览 随着实时图形研究的进步及主流GPU能力的提升,我们见证了创新算法在渲染复杂虚拟世界的交互速率上的显著成效。每年,最新的电子游戏都会展示出一系列复杂的算法,这些算法推动了3D...

    Rendering rapids in Uncharted 4.pdf

    《神秘海域4:盗贼末路》(Uncharted 4)作为一款第三人称动作冒险游戏,玩家可以在其中体验到全球各地的异国风情,寻找冒险和宝藏。虽然游戏本身并不主打水元素,但是水——以各种形态呈现——一直是游戏中一个关键...

    Real-time Rendering Tricks and Techniques in DirectX代码例子

    以上都是在"Real-time Rendering Tricks and Techniques in DirectX"这个主题下可能涵盖的一些关键概念。通过深入理解和应用这些技术,开发者能够构建出流畅、高质量的实时渲染应用程序。在压缩包中的"bookcode"文件...

    KeyShot Network Rendering使用方法

    《KeyShot Network Rendering 使用详解与常见问题解决》 KeyShot Network Rendering 是一款强大的网络渲染工具,专为提高渲染效率而设计。它允许用户利用多台计算机的计算资源,协同完成复杂的3D场景渲染任务,尤其...

    Digital Lighting & Rendering

    Crafting a perfect rendering in 3D software means nailing all the details. And no matter what software you use, your success in creating realistic-looking illumination, shadows and textures depends on...

    Real-Time Rendering Tricks and Techniques in DirectX.

    本书《实时渲染技巧与技术在DirectX中的应用》(Real-Time Rendering Tricks and Techniques in DirectX)是一本全面介绍如何利用DirectX进行实时图形渲染的专业书籍。DirectX是微软开发的一套多媒体编程接口,被...

    Blender 2.5 Lighting and Rendering.pdf

    ### Blender 2.5 Lighting and Rendering #### 一、引言 《Blender 2.5 Lighting and Rendering》是一本由Aaron W. Powell撰写的书籍,该书详细介绍了如何使用Blender 2.5版本进行照明和渲染的技术。本书不仅为初学...

    Unstructured Lumigraph Rendering.PDF

    Unstructured Lumigraph Rendering.PDF

    Physically Based Rendering第3版

    Algorithms for animation, geometric modeling, and texturing all must feed their results through some sort of rendering process for the results to be visible in an image. PBR渲染的一本极好的书籍

    rails-beginner-s-guide

    Rails中的渲染(Rendering)是另一个重要知识点,涵盖了渲染参数的汇总与详解,以及Rails的其他渲染相关方法。渲染是将数据转换为HTML或其他格式并输出给客户端的过程。Rails提供多种渲染方法,包括渲染HTML模板、...

    Agile Web Development with Rails Final

    Ruby on Rails (often shortened as Rails) is a server-side web application framework written in Ruby under the MIT License. It uses Model-View-Controller (MVC) architecture and emphasizes convention ...

    Real-Time Rendering 4rd全彩高清

    Real-Time Rendering 4rd最新版,高清大概800M,详细介绍渲染知识

Global site tag (gtag.js) - Google Analytics