• ruby on rails 中render的使用


    render 先上点搜集的常用方式

    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)

    例如 <%= 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的源码,粘贴出来如下:

    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) andwhen 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 orfor 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} "
    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 :updatedo |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 :statusand: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) 
  • 相关阅读:
    Problem about objc_exception_throw
    Change mime Types for mac's default apache
    重启Mac上的Apache服务
    通过无线网安装自己打包的ipa文件
    How to cancel selection for TreePanel(GXT)
    使用Jsoup获取网页内容超时设置
    HTML Meta, http-equiv, Refresh
    线上redis服务内存异常分析。
    C++/CLI 本地字符串和托管字符串之间的转换
    DWF Toolkit on Microsoft Windows
  • 原文地址:https://www.cnblogs.com/wangyuyu/p/3235597.html
Copyright © 2020-2023  润新知