render 先上點蒐集的經常使用方式javascript
- 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 :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文件html
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() java
第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)web
查了render的源碼,粘貼出來以下:json
- 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).
-
-
- render :action => "goal"
-
-
-
- render :action => "short_goal", :layout => false
-
-
-
- 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) andwhen sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.
-
-
- render :partial => "person", :locals => { :name => "david" }
-
-
-
- render :partial => "person", :object => @new_person
-
-
-
-
- render :partial => "person", :collection => @winners
-
-
- render :partial => "admin_person", :collection => @winners, :as => :person
-
-
-
- render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
-
-
-
-
-
- render :partial => "shared/note", :collection => @new_notes
-
-
- 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.
-
-
- render :template => "weblog/show"
-
-
- 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.
-
-
- render :file => "/path/to/some/template.erb"
- render :file => "c:/path/to/some/template.erb"
-
-
- 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 orfor rendering prepared content, such as a cache. By default, text rendering is not done within the active layout.
-
-
- render :text => "hello world!"
-
-
- render :text => "Explosion!", :status => 500
-
-
- render :text => "Hi there!", :layout => true
-
-
-
- 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
-
- The following example demonstrates how one can stream a large amount of on-the-fly 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:
-
-
- render :text => proc { |response, output| output.write("Hello from code!") }
-
- Rendering XML
-
- Rendering XML sets the content type to application/xml.
-
-
- 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:
-
-
- 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.
-
-
- 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:
-
-
- 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.
-
-
- 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.
-
-
- render :inline => "<%= 'hello, ' * 3 + 'again' %>"
-
-
- render :inline => "xml.p { 'Good seeing you!' }", :type => :builder
-
-
- 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 :updatedo |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.
-
-
- render :js => "alert('hello')"
-
- Rendering with status and location headers
-
- All renders take the :statusand:location options and turn them into headers. They can even be used together:
-
- render :xml => post.to_xml, :status => :created, :location => post_url(post)