最近写ror,因为比较菜,很多东西不知道,只能看一点查一点了

render 先上点搜集的常用方式

render :action => "long_goal", :layout => "spectacular"
render :partial => "person", :locals => { :name => "david" }
render :template => "weblog/show", :locals => {:customer => Customer.new}
render :file => "c:/path/to/some/template.erb", :layout => true, :status => 404
render :text => "Hi there!", :layout => "special"
render :text => proc { |response, output| output.write("Hello from code!") }
render :xml => {:name => "David"}.to_xml
render :json => {:name => "David"}.to_json, :callback => 'show'
render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" }
render :js => "alert('hello')"
render :xml => post.to_xml, :status => :created, :location => post_url(post)

例如 <%= render 'form' %> 就是 跳转到 _form.html.erb文件

1:render(:text => string)   
2:render(:inline => string, 
3:[:type => "rhtml"|"rxml"])   
4:render(:action => action_name)   
5:render(:file => path, 
6:[:use_full_path => true|false])   
7:render(:template => name)   
8:render(:partial => name)   
9:render(:nothing=>true)   
10:render()

第1行:直接渲染出文本 
第2行:把传入的string渲染成模板(rhtml或者rxml) 
第3行:直接调用某个action的模板,相当于forward到一个view 
第4行:使用某个模板文件render, 当use_full_path参数为true时可以传入相对路径 
第5行:使用模板名render,e.x.: render(:template => "blog/short_list") 
第6行:以局部模板渲染 
第7行:什么也不输出,包括layout 
第8行:默认的的render, 相当于render(:action => self)

查了render的源码,粘贴出来如下:

Renders the content that will be returned to the browser as the response body.
Rendering an action Action rendering is the most common form and the type used automatically by Action Controller when nothing else is specified. By default, actions are rendered within the current layout (if one exists). # Renders the template for the action "goal" within the current controller
render :action => "goal" # Renders the template for the action "short_goal" within the current controller,
# but without the current active layout
render :action => "short_goal", :layout => false # Renders the template for the action "long_goal" within the current controller,
# but with a custom layout
render :action => "long_goal", :layout => "spectacular" Rendering partials Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used. # Renders the same partial with a local variable.
render :partial => "person", :locals => { :name => "david" } # Renders the partial, making @new_person available through
# the local variable 'person'
render :partial => "person", :object => @new_person # Renders a collection of the same partial by making each element
# of @winners available through the local variable "person" as it
# builds the complete response.
render :partial => "person", :collection => @winners # Renders a collection of partials but with a custom local variable name
render :partial => "admin_person", :collection => @winners, :as => :person # Renders the same collection of partials, but also renders the
# person_divider partial between each person partial.
render :partial => "person", :collection => @winners, :spacer_template => "person_divider" # Renders a collection of partials located in a view subfolder
# outside of our current controller. In this example we will be
# rendering app/views/shared/_note.r(html|xml) Inside the partial
# each element of @new_notes is available as the local var "note".
render :partial => "shared/note", :collection => @new_notes # Renders the partial with a status code of 500 (internal error).
render :partial => "broken", :status => 500 Note that the partial filename must also be a valid Ruby variable name, so e.g. 2005 and register-user are invalid.
Automatic etagging Rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified and the response body will be set to an empty string. No etag header will be inserted if it‘s already set.
Rendering a template Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied. # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show" # Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new} Rendering a file File rendering works just like action rendering except that it takes a filesystem path. By default, the path is assumed to be absolute, and the current layout is not applied. # Renders the template located at the absolute filesystem path
render :file => "/path/to/some/template.erb"
render :file => "c:/path/to/some/template.erb" # Renders a template within the current layout, and with a 404 status code
render :file => "/path/to/some/template.erb", :layout => true, :status => 404
render :file => "c:/path/to/some/template.erb", :layout => true, :status => 404 Rendering text Rendering of text is usually used for tests or for rendering prepared content, such as a cache. By default, text rendering is not done within the active layout. # Renders the clear text "hello world" with status code 200
render :text => "hello world!" # Renders the clear text "Explosion!" with status code 500
render :text => "Explosion!", :status => 500 # Renders the clear text "Hi there!" within the current active layout (if one exists)
render :text => "Hi there!", :layout => true # Renders the clear text "Hi there!" within the layout
# placed in "app/views/layouts/special.r(html|xml)"
render :text => "Hi there!", :layout => "special" Streaming data and/or controlling the page generation The :text option can also accept a Proc object, which can be used to: 1. stream on-the-fly generated data to the browser. Note that you should use the methods provided by ActionController::Steaming instead if you want to stream a buffer or a file.
2. manually control the page generation. This should generally be avoided, as it violates the separation between code and content, and because almost everything that can be done with this method can also be done more cleanly using one of the other rendering methods, most notably templates. Two arguments are passed to the proc, a response object and an output object. The response object is equivalent to the return value of the ActionController::Base#response method, and can be used to control various things in the HTTP response, such as setting the Content-Type header. The output object is an writable IO-like object, so one can call write and flush on it. The following example demonstrates how one can stream a large amount of on-the-fly generated data to the browser: # Streams about 180 MB of generated data to the browser.
render :text => proc { |response, output|
10_000_000.times do |i|
output.write("This is line #{i}\n")
output.flush
end
} Another example: # Renders "Hello from code!"
render :text => proc { |response, output| output.write("Hello from code!") } Rendering XML Rendering XML sets the content type to application/xml. # Renders '<name>David</name>'
render :xml => {:name => "David"}.to_xml It‘s not necessary to call to_xml on the object you want to render, since render will automatically do that for you: # Also renders '<name>David</name>'
render :xml => {:name => "David"} Rendering JSON Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected that the response will be parsed (or eval‘d) for use as a data structure. # Renders '{"name": "David"}'
render :json => {:name => "David"}.to_json It‘s not necessary to call to_json on the object you want to render, since render will automatically do that for you: # Also renders '{"name": "David"}'
render :json => {:name => "David"} Sometimes the result isn‘t handled directly by a script (such as when the request comes from a SCRIPT tag), so the :callback option is provided for these cases. # Renders 'show({"name": "David"})'
render :json => {:name => "David"}.to_json, :callback => 'show' Rendering an inline template Rendering of an inline template works as a cross between text and action rendering where the source for the template is supplied inline, like text, but its interpreted with ERb or Builder, like action. By default, ERb is used for rendering and the current layout is not used. # Renders "hello, hello, hello, again"
render :inline => "<%= 'hello, ' * 3 + 'again' %>" # Renders "<p>Good seeing you!</p>" using Builder
render :inline => "xml.p { 'Good seeing you!' }", :type => :builder # Renders "hello david"
render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" } Rendering inline JavaScriptGenerator page updates In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details), you can also pass the :update parameter to render, along with a block, to render page updates inline. render :update do |page|
page.replace_html 'user_list', :partial => 'user', :collection => @users
page.visual_effect :highlight, 'user_list'
end Rendering vanilla JavaScript In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js. # Renders "alert('hello')" and sets the mime type to text/javascript
render :js => "alert('hello')" Rendering with status and location headers All renders take the :status and :location options and turn them into headers. They can even be used together: render :xml => post.to_xml, :status => :created, :location => post_url(post)

ruby on rails 中render的使用的更多相关文章

  1. ruby on rails 中render的

    Ruby rails页面跳转代码如下: 1.render(:text => string) 2.render(:inline => string, [:type => "r ...

  2. 【转】Ruby on Rails中select使用方法

    在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆.常见的有三个..select, select_tag, collection_select(其余的什么sel ...

  3. Ruby on Rails中的Rake教程(Rake如何把我灌醉!)

    下面是我们使用Rake任务的例子: 1.给列表中的用户发送邮件 2.每晚数据的计算和报告 3.过期或重新生成缓存 4.备份数据和svn版本(how's this : subversion reposi ...

  4. json格式在ruby和rails中的注意事项

    #虚拟网络拓扑的json数据 def topodata #@vnic = Vnic.all #flash.now[:notice] = 'Message sent!' #flash.now[:aler ...

  5. 理解ruby on rails中的ActiveRecord::Relation

    ActiveRecord::Relation是rails3中添加的.rails2中的finders, named_scope, with_scope 等用法,在rails3统一为一种Relation用 ...

  6. rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each' for #...

    在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views ...

  7. Rails中render和redirect_to的区别

    共同点: render 和redirect_to 都是执行页面跳转,但是,写在这两个方法后面的语句仍然会被执行. 不同: render:简单的页面渲染,可以指定渲染的页面或布局文件,但是不会发出请求, ...

  8. Ruby on Rails 中你使用了Kaminari 后,千万不要再引入will_pagination 这个Gem 了

    今日做开发的时候发现的这个问题 发现无论怎样配置都不能使用Kaminari 的Per 这个功能,分页大小也固定在了30 最开始还以为是Ransack 这个Gem 影响的,上网搜了很久发现没有 最后仔细 ...

  9. ruby on rails模拟HTTP请求错误发生:end of file reached

    在文章 Ruby On Rails中REST API使用演示样例--基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求訪问IBM Bluemix上的sour ...

随机推荐

  1. 在CentOS6上使用YUM安装Mysql5.5.x

    1.安装MySQL 5.5.x的yum源: rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm 2.安装MySQL客户端的支持包: yu ...

  2. 至芯FPGA培训中心-1天FPGA设计集训(赠送FPGA开发板)

    至芯FPGA培训中心-1天FPGA设计集训(赠送开发板) 开课时间2014年5月3日 课程介绍 FPGA设计初级培训班是针对于FPGA设计技术初学者的课程.课程不仅是对FPGA结构资源和设计流程的描述 ...

  3. [转]C# FileSystemWatcher监控指定文件或目录的文件的创建、删除、改动、重命名等活动

    觉得这个很常用..比如一些软件.   http://www.rabbit8.cn/DoNet/407.html   FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删 ...

  4. Windows消息编程(写的不错,有前因后果)

    本文主要包括以下内容: 1.简单理解Windows的消息2.通过一个简单的Win32程序理解Windows消息3.通过几个Win32程序实例进一步深入理解Windows消息4.队列消息和非队列消息5. ...

  5. Android网络传输中必用的两个加密算法:MD5 和 RSA

    MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...

  6. bzoj3573[Hnoi2014]米特运输

    http://www.lydsy.com/JudgeOnline/problem.php?id=3573 好吧,虽然这是day1最后一题,但却是最水的一题....(前提:看懂题目) 仔细看题! 仔细看 ...

  7. C#中枚举类型和int类型的转化

    先定义一个枚举类型 , 初中, 高中,大学 }; int ->enum int d=2; PropertyType  a=(PropertyType)d; int <- enum Prop ...

  8. 在sql语句中使用plsql变量

    示例代码如下: create or replace type ua_id_table is table of number; declare v_tab ua_id_table;begin v_tab ...

  9. HTTP学习笔记4-请求与响应结构例子

    18,HTTP消息由客户端到服务器的请求和服务器到客户端的响应组成.请求消息和响应消息都是由开始行,消息报头(可选的),空行(只有CRLF的行),消息正文(可选的)组成. 19,对于请求消息,开始行就 ...

  10. 使用jquery生成二维码

    二维码已经渗透到生活中的方方面面,不管到哪,我们都可以用扫一扫解决大多数问题.二狗为了准备应对以后项目中会出现的二维码任务,就上网了解了如何使用jquery.qrcode生成二维码.方法简单粗暴,[] ...