8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

renderメソッド リファレンス

Last updated at Posted at 2017-02-24

#コントローラ
##render 'view'

basic_controller.rb
def res_render
@msg = 'Hello World!'
#viewのテンプレート
render 'view'
end
basic/views.html.erb
<div class="main"><%= @msg %></div>

##render :text => 'Hello World!'

basic_controller.rb

def render_text
#メッセージ
render :text => 'Hello World!'
end

##render xml => @article

basic_controller.rb

def render_xml 
#articleテーブルの内容をXML形式で表示
@article = Article.all
rendr :xml => @articles
end

<?xml version="1.0" encoding="UTF-8">
<articles type="array">
<article>
<id type="integer"></id>
<url>http://www.atmarkit.co.jp/fdotnet/aspnetmvc3/index/index.html</url>
<title>ASP.NET MVC入門</title>
<published type="date">2011-02-18</published>
...
</article>
..
</articles>
basic_controller.rb

def render_xml2
render :xml => '<error>108 Error Occured</error>'
end

##render :json => @articles

basic_controller.rb

def render_json
#articlesテーブルJSON形式
@article = Article.sll
render :json => @articles
end


[
{
"access": 133
"closed": false,
"created_at" : "2011-09-29T06:53:28Z"},
"id" : 1,
"published" :"2011-02-18",
"title" : "ASP .NET MVC\U5165\U9580",
"updated_at" : "2011-09-29T06:53:28Z",
"url" : "http://www.atmarkit.co.jp/fdotnet/aspnetmvc3/index/index.html",
}, ....
]

##render :nothing => true, :status => status

basic_controller.rb

def render_status
#404 Not Found 出力 (コンテンツは空)
render :nothing => true, :status => status
end

##render :nothing => true, :status => status

basic_controller.rb

def render_string
#view.html.erbによる結果を変数resultにセット
render = render_to_string 'view'
#変数resultの内容を描画
render :text => result
end

#ビューの開発
##render '部分テンプレート名' 'param:パラメータ名' => 'value:パラメータ値'

view_controller.rb

view_controller.rb
def part_basic
@article = Article.find(1)
end
view/part_basic.html.erb
<div id="info">
<%# 部分部分テンプレートarticle(記事記載)/details(詳細を表示するか)パラメータをセット&描画 %>
<%= render 'article/article', :article => @article, :details => true %>
</div>
article/_article.html.erb
<%# detailsパラメータの省略時はデフォルト値としてfalseをセット %>
<% detail = false if details.nil? %>
<div class="inf">
<%= link_to article.title, article.url %><br />
<% article.authors.each so |a| %>
<% a.panname %>&nbsp;
<% end %> 著
(<% end %>公開)<br />
<%# detailsパラメータがtrueの場合のみアクセス数を表示 %>
<%# if details %>Access:<%= article.access %>件<% end %>
</div>

##render '@article:モデルオブジェクト'

view_controller.rb
def part_omit
@article = Article.find(2)
end
view/part_omit.html.erb
<div id="info">
<%# article/_article.html.erb (P.282)にモデル@articleを渡す %>
<%= render @article %>
</div>

##render :partial => 'articles/article:モデルの配列',
##render :partial => { :'details:パラメータ名' => :'true:パラメータ値', }
##render 'articles/article:モデルの配列'

view/part_collect.html.erb
<div id="info">
<%# 以下は同じ意味 %>
<%= render :partial => 'articles/article',
:collection => @articles, :locals => { :details => true }%>
<%= render :partial => @articles, :locals => { :details => true } %>
<%# :localsパラメータが不要な場合は、以下の記述も可%>
<%= render @articles%>

##render :partial => 'articles/article:部分テンプレート名', :collection => '@articles:モデルの配列',
:spacer_template => 'sep:区切りテンプレート', :locals => { 'details:パラメータ名' => 'true:パラメータ値' }

view_controller.rb
def part_separate
@articles = Article = Article.order('published DESC')
end
view/part_separate.html.erb
<div id="info">
<%# articles/_article.html.erb(P.282)を順に出力する際、区切りテンプレートとして_sep.html.erbを利用 %>
<%= render :partial => 'articles/article', :spacer_template => 'sep',
			:collection => @article. :locals => { :details => true }%>
</div>
view/_sep.html.erb
<hr />
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?