关于Render在不同情况的用法

render是一个个人比较喜欢工具,先列一些常用的吧

Ruby代码 复制代码  收藏代码
  1. render :action => "long_goal":layout => "spectacular"  
  2. render :partial => "person":locals => { :name => "david" }   
  3. render :template => "weblog/show":locals => {:customer => Customer.new}   
  4. render :file => "c:/path/to/some/template.erb":layout => true:status => 404   
  5. render :text => "Hi there!":layout => "special"  
  6. render :text => proc { |response, output| output.write("Hello from code!") }   
  7. render :xml => {:name => "David"}.to_xml   
  8. render :json => {:name => "David"}.to_json, :callback => 'show'  
  9. render :inline => "<%= 'hello ' + name %>":locals => { :name => "david" }   
  10. render :js => "alert('hello')"  
  11. render :xml => post.to_xml, :status => :created:location => post_url(post)  
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)


放到这里,用的时候好找,呵呵

Ruby代码 复制代码  收藏代码
  1. Renders the content that will be returned to the browser as the response body.   
  2. Rendering an action   
  3.   
  4. 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).   
  5.   
  6.   # Renders the template for the action "goal" within the current controller   
  7.   render :action => "goal"  
  8.   
  9.   # Renders the template for the action "short_goal" within the current controller,   
  10.   # but without the current active layout   
  11.   render :action => "short_goal":layout => false  
  12.   
  13.   # Renders the template for the action "long_goal" within the current controller,   
  14.   # but with a custom layout   
  15.   render :action => "long_goal":layout => "spectacular"  
  16.   
  17. Rendering partials   
  18.   
  19. 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.   
  20.   
  21.   # Renders the same partial with a local variable.   
  22.   render :partial => "person":locals => { :name => "david" }   
  23.   
  24.   # Renders the partial, making @new_person available through   
  25.   # the local variable 'person'   
  26.   render :partial => "person":object => @new_person  
  27.   
  28.   # Renders a collection of the same partial by making each element   
  29.   # of @winners available through the local variable "person" as it   
  30.   # builds the complete response.   
  31.   render :partial => "person":collection => @winners  
  32.   
  33.   # Renders a collection of partials but with a custom local variable name   
  34.   render :partial => "admin_person":collection => @winners:as => :person  
  35.   
  36.   # Renders the same collection of partials, but also renders the   
  37.   # person_divider partial between each person partial.   
  38.   render :partial => "person":collection => @winners:spacer_template => "person_divider"  
  39.   
  40.   # Renders a collection of partials located in a view subfolder   
  41.   # outside of our current controller.  In this example we will be   
  42.   # rendering app/views/shared/_note.r(html|xml)  Inside the partial   
  43.   # each element of @new_notes is available as the local var "note".   
  44.   render :partial => "shared/note":collection => @new_notes  
  45.   
  46.   # Renders the partial with a status code of 500 (internal error).   
  47.   render :partial => "broken":status => 500   
  48.   
  49. Note that the partial filename must also be a valid Ruby variable name, so e.g. 2005 and register-user are invalid.   
  50. Automatic etagging   
  51.   
  52. 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.   
  53. Rendering a template   
  54.   
  55. Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.   
  56.   
  57.   # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)   
  58.   render :template => "weblog/show"  
  59.   
  60.   # Renders the template with a local variable   
  61.   render :template => "weblog/show":locals => {:customer => Customer.new}   
  62.   
  63. Rendering a file   
  64.   
  65. 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.   
  66.   
  67.   # Renders the template located at the absolute filesystem path   
  68.   render :file => "/path/to/some/template.erb"  
  69.   render :file => "c:/path/to/some/template.erb"  
  70.   
  71.   # Renders a template within the current layout, and with a 404 status code   
  72.   render :file => "/path/to/some/template.erb":layout => true:status => 404   
  73.   render :file => "c:/path/to/some/template.erb":layout => true:status => 404   
  74.   
  75. Rendering text   
  76.   
  77. 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.   
  78.   
  79.   # Renders the clear text "hello world" with status code 200   
  80.   render :text => "hello world!"  
  81.   
  82.   # Renders the clear text "Explosion!"  with status code 500   
  83.   render :text => "Explosion!":status => 500   
  84.   
  85.   # Renders the clear text "Hi there!" within the current active layout (if one exists)   
  86.   render :text => "Hi there!":layout => true  
  87.   
  88.   # Renders the clear text "Hi there!" within the layout   
  89.   # placed in "app/views/layouts/special.r(html|xml)"   
  90.   render :text => "Hi there!":layout => "special"  
  91.   
  92. Streaming data and/or controlling the page generation   
  93.   
  94. The :text option can also accept a Proc object, which can be used to:   
  95.   
  96.    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.   
  97.    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.   
  98.   
  99. 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.   
  100.   
  101. The following example demonstrates how one can stream a large amount of on-the-fly generated data to the browser:   
  102.   
  103.   # Streams about 180 MB of generated data to the browser.   
  104.   render :text => proc { |response, output|   
  105.     10_000_000.times do |i|   
  106.       output.write("This is line #{i}\n")   
  107.       output.flush   
  108.     end  
  109.   }   
  110.   
  111. Another example:   
  112.   
  113.   # Renders "Hello from code!"   
  114.   render :text => proc { |response, output| output.write("Hello from code!") }   
  115.   
  116. Rendering XML   
  117.   
  118. Rendering XML sets the content type to application/xml.   
  119.   
  120.   # Renders '<name>David</name>'   
  121.   render :xml => {:name => "David"}.to_xml   
  122.   
  123. It‘s not necessary to call to_xml on the object you want to render, since render will automatically do that for you:   
  124.   
  125.   # Also renders '<name>David</name>'   
  126.   render :xml => {:name => "David"}   
  127.   
  128. Rendering JSON   
  129.   
  130. 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.   
  131.   
  132.   # Renders '{"name": "David"}'   
  133.   render :json => {:name => "David"}.to_json   
  134.   
  135. It‘s not necessary to call to_json on the object you want to render, since render will automatically do that for you:   
  136.   
  137.   # Also renders '{"name": "David"}'   
  138.   render :json => {:name => "David"}   
  139.   
  140. 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.   
  141.   
  142.   # Renders 'show({"name": "David"})'   
  143.   render :json => {:name => "David"}.to_json, :callback => 'show'  
  144.   
  145. Rendering an inline template   
  146.   
  147. 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.   
  148.   
  149.   # Renders "hello, hello, hello, again"   
  150.   render :inline => "<%= 'hello, ' * 3 + 'again' %>"  
  151.   
  152.   # Renders "<p>Good seeing you!</p>" using Builder   
  153.   render :inline => "xml.p { 'Good seeing you!' }":type => :builder  
  154.   
  155.   # Renders "hello david"   
  156.   render :inline => "<%= 'hello ' + name %>":locals => { :name => "david" }   
  157.   
  158. Rendering inline JavaScriptGenerator page updates   
  159.   
  160. 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.   
  161.   
  162.   render :update do |page|   
  163.     page.replace_html  'user_list':partial => 'user':collection => @users  
  164.     page.visual_effect :highlight'user_list'  
  165.   end  
  166.   
  167. Rendering vanilla JavaScript   
  168.   
  169. In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js.   
  170.   
  171.   # Renders "alert('hello')" and sets the mime type to text/javascript   
  172.   render :js => "alert('hello')"  
  173.   
  174. Rendering with status and location headers   
  175.   
  176. All renders take the :status and :location options and turn them into headers. They can even be used together:   
  177.   
  178.   render :xml => post.to_xml, :status => :created:location => post_url(post)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值