ruby on rails 中render的使用

后端开发   发布日期:2025年07月12日   浏览次数:127

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

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 '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. 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).
  4. # Renders the template for the action "goal" within the current controller
  5. render :action => "goal"
  6. # Renders the template for the action "short_goal" within the current controller,
  7. # but without the current active layout
  8. render :action => "short_goal", :layout => false
  9. # Renders the template for the action "long_goal" within the current controller,
  10. # but with a custom layout
  11. render :action => "long_goal", :layout => "spectacular"
  12. Rendering partials
  13. 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.
  14. # Renders the same partial with a local variable.
  15. render :partial => "person", :locals => { :name => "david" }
  16. # Renders the partial, making @new_person available through
  17. # the local variable 'person'
  18. render :partial => "person", :object => @new_person
  19. # Renders a collection of the same partial by making each element
  20. # of @winners available through the local variable "person" as it
  21. # builds the complete response.
  22. render :partial => "person", :collection => @winners
  23. # Renders a collection of partials but with a custom local variable name
  24. render :partial => "admin_person", :collection => @winners, :as => :person
  25. # Renders the same collection of partials, but also renders the
  26. # person_divider partial between each person partial.
  27. render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
  28. # Renders a collection of partials located in a view subfolder
  29. # outside of our current controller. In this example we will be
  30. # rendering app/views/shared/_note.r(html|xml) Inside the partial
  31. # each element of @new_notes is available as the local var "note".
  32. render :partial => "shared/note", :collection => @new_notes
  33. # Renders the partial with a status code of 500 (internal error).
  34. render :partial => "broken", :status => 500
  35. Note that the partial filename must also be a valid Ruby variable name, so e.g. 2005 and register-user are invalid.
  36. Automatic etagging
  37. 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 its already set.
  38. Rendering a template
  39. Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.
  40. # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
  41. render :template => "weblog/show"
  42. # Renders the template with a local variable
  43. render :template => "weblog/show", :locals => {:customer => Customer.new}
  44. Rendering a file
  45. 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.
  46. # Renders the template located at the absolute filesystem path
  47. render :file => "/path/to/some/template.erb"
  48. render :file => "c:/path/to/some/template.erb"
  49. # Renders a template within the current layout, and with a 404 status code
  50. render :file => "/path/to/some/template.erb", :layout => true, :status => 404
  51. render :file => "c:/path/to/some/template.erb", :layout => true, :status => 404
  52. Rendering text
  53. 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.
  54. # Renders the clear text "hello world" with status code 200
  55. render :text => "hello world!"
  56. # Renders the clear text "Explosion!" with status code 500
  57. render :text => "Explosion!", :status => 500
  58. # Renders the clear text "Hi there!" within the current active layout (if one exists)
  59. render :text => "Hi there!", :layout => true
  60. # Renders the clear text "Hi there!" within the layout
  61. # placed in "app/views/layouts/special.r(html|xml)"
  62. render :text => "Hi there!", :layout => "special"
  63. Streaming data and/or controlling the page generation
  64. The :text option can also accept a Proc object, which can be used to:
  65. 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.
  66. 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.
  67. 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.
  68. The following example demonstrates how one can stream a large amount of on-the-fly generated data to the browser:
  69. # Streams about 180 MB of generated data to the browser.
  70. render :text => proc { |response, output|
  71. 10_000_000.times do |i|
  72. output.write("This is line #{i}\n")
  73. output.flush
  74. end
  75. }
  76. Another example:
  77. # Renders "Hello from code!"
  78. render :text => proc { |response, output| output.write("Hello from code!") }
  79. Rendering XML
  80. Rendering XML sets the content type to application/xml.
  81. # Renders '<name>David</name>'
  82. render :xml => {:name => "David"}.to_xml
  83. Its not necessary to call to_xml on the object you want to render, since render will automatically do that for you:
  84. # Also renders '<name>David</name>'
  85. render :xml => {:name => "David"}
  86. Rendering JSON
  87. 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 evald) for use as a data structure.
  88. # Renders '{"name": "David"}'
  89. render :json => {:name => "David"}.to_json
  90. Its not necessary to call to_json on the object you want to render, since render will automatically do that for you:
  91. # Also renders '{"name": "David"}'
  92. render :json => {:name => "David"}
  93. Sometimes the result isnt handled directly by a script (such as when the request comes from a SCRIPT tag), so the :callback option is provided for these cases.
  94. # Renders 'show({"name": "David"})'
  95. render :json => {:name => "David"}.to_json, :callback => 'show'
  96. Rendering an inline template
  97. 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.
  98. # Renders "hello, hello, hello, again"
  99. render :inline => "<%= 'hello, ' * 3 + 'again' %>"
  100. # Renders "<p>Good seeing you!</p>" using Builder
  101. render :inline => "xml.p { 'Good seeing you!' }", :type => :builder
  102. # Renders "hello david"
  103. render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" }
  104. Rendering inline JavaScriptGenerator page updates
  105. 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.
  106. render :update do |page|
  107. page.replace_html 'user_list', :partial => 'user', :collection => @users
  108. page.visual_effect :highlight, 'user_list'
  109. end
  110. Rendering vanilla JavaScript
  111. In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js.
  112. # Renders "alert('hello')" and sets the mime type to text/javascript
  113. render :js => "alert('hello')"
  114. Rendering with status and location headers
  115. All renders take the :status and :location options and turn them into headers. They can even be used together:
  116. render :xml => post.to_xml, :status => :created, :location => post_url(post)


 

 

以上就是ruby on rails 中render的使用的详细内容,更多关于ruby on rails 中render的使用的资料请关注九品源码其它相关文章!