Ruby-on-rails-2.1-rails-render

提供:Dev Guides
移動先:案内検索

Ruby on Rails 2.1-レンダリング

通常、コントローラーメソッドと同じ名前のビューテンプレートを使用して結果をレンダリングします。

アクション

# 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'

部分的

パーシャルは、「_ 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'

テンプレート

これは、アクションのレンダリングに似ていますが、テンプレートルート(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'

インラインテンプレート

ERbを使用して「ミニチュア」テンプレートをレンダリングします。

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

何もない

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

コンテンツタイプを変更します。

render :action => "atom.rxml",
   :content_type => "application/atom+xml"