Ruby on Rails 2.1 - Render



Usually the view template with the same name as the controller method is used to render the results.

Action

# The default. Does not need to be specified 
# in a controller method called "some_action"
render :action => 'some_action'   
render :action => 'another_action', :layout => false
render :action => 'some_action', :layout => 'another_layout'

Partial

Partials are stored in files called "_subformname" ( _error, _subform, _listitem).

render :partial => 'subform'
render :partial => 'error', :status => 500
render :partial => 'subform', :locals => 
                   { :variable => @other_variable }
render :partial => 'listitem', :collection => @list
render :partial => 'listitem', :collection => @list, 
                   :spacer_template => 'list_divider'

Template

It is more like rendering an action, but it finds the template based on the template root (app/views).

# renders app/views/weblog/show
render :template => 'weblog/show' 

File

render :file => '/path/to/some/file.rhtml'
render :file => '/path/to/some/filenotfound.rhtml', 
                    status => 404, :layout => true

Text

render :text => "Hello World"
render :text => "This is an error", :status => 500
render :text => "Let's use a layout", :layout => true
render :text => 'Specific layout', :layout => 'special'

Inline Template

Uses ERb to render the "miniature" template.

render :inline => "<%= 'hello , ' * 3 + 'again' %>"
render :inline => "<%= 'hello ' + name %>", 
   :locals => { :name => "david" }

Nothing

render :nothing
render :nothing, :status => 403    # forbidden

RJS

def refresh
   render :update do |page|
      page.replace_html  'user_list', :partial => 'user', 
         :collection => @users
      page.visual_effect :highlight, 'user_list'
   end
end

Change the content-type:

render :action => "atom.rxml", 
   :content_type => "application/atom+xml"
rails-quick-guide.htm
Advertisements