Railsでviewを表示してみる
前回、空のindexを作成し、viewを表示するところまで確認。
renderを省略した場合
「アクション名.html.erb」のテンプレートを表示する場合は、renderは省略できる。
$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController def index end end
$ vi app/views/sample/index.html.erb
<h1>Hello, world!</h1>
=>画面には「Hello, world!」と表示される。
変数を渡す
インスタンス変数に指定した値がそのままviewで参照できる。
$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController def index @var = "Hello, world! 2" end end
$ vi app/views/sample/index.html.erb
<h1><%= @var %></h1>
=>画面には「Hello, world! 2」と表示される。
レイアウトでも同じように変数を参照できる。
$ vi app/views/layouts/application.html.erb
<!DOCTYPE html> <html> <head> <title><%= @var %></title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <%= yield %> </body> </html>
=>タイトルが「Hello, world! 2」になる。
viewを変更する
renderにactionを指定すると、viewを変更できる。
(:actionは省略することも可能)
$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController def index render :action => "index2" # render "index2" でもOK end end
$ vi app/views/sample/index2.html.erb
<h1>Hello, world! 3</h1>
=>画面には「Hello, world! 3」と表示される。
レイアウトも変更する場合はlayoutを指定する。
$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController def index render :action => "index2", :layout => "application2" end end
$ vi app/views/layouts/application2.html.erb
<!DOCTYPE html> <html> <head> <title>Hello, world! 3</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <%= yield %> </body> </html>
=>タイトルが「Hello, world! 3」になる。
content_for
content_forを使うと、viewからレイアウトにコンテンツを渡せる。
$ vi app/views/sample/index.html.erb
<% content_for :content_sample do %> aaa<br /> bbb<br /> ccc<br /> <% end %> <h1>Hello, world!</h1>
$ vi app/views/layouts/application.html.erb
<!DOCTYPE html> <html> <head> <title>Sample</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <%= yield %> <%= yield :content_sample %> </body> </html>
=>「Hello, world!」の下に
aaa
bbb
ccc
が表示される。
部分テンプレート
Viewの中でrenderを使うと部分テンプレートを表示できる。
(:partialは省略することも可能)
$ vi app/views/sample/index.html.erb
<h1>Hello, world</h1> <%= render :partial => 'sample' %> <!-- render 'sample' でもOK -->
$ vi app/views/sample/_sample.html.erb
ddd<br /> eee<br /> fff<br />
=>「Hello, world!」の下に
ddd
eee
fff
が表示される。
部分テンプレートには変数を渡すこともできる。
$ vi app/views/sample/index.html.erb
<h1>Hello, world</h1> <%= render :partial => 'sample', :locals => {:var => 2} %>
$ vi app/views/sample/_sample.html.erb
ddd<%= var %><br /> fff<%= var %><br /> ggg<%= var %><br />
=>「Hello, world!」の下に
ddd2
eee2
fff2
が表示される。
参考
render - リファレンス - Railsドキュメント
http://railsdoc.com/references/render
content_for - リファレンス - Railsドキュメント
http://railsdoc.com/references/content_for