Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | particalでViewの部分レイアウトを適用する

概要

particalでViewの部分レイアウトを適用する

内容

メニューなど、部分的なレイアウトを利用したい場合は
部分テンプレートを利用します。

=render :partial => "folder/template_name"

template_nameは実際のファイル名側のみアンダースコアを付与して
部分テンプレートであることを明示します。
呼び出し側から指定する場合はアンダースコア不要です。

サンプル

view/layouts/application.html.haml
!!!
%html
  %head
    %title Rails4demo
    = stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true
    = javascript_include_tag "application", "data-turbolinks-track" => true
    = csrf_meta_tags
  %body
    - if yield(:title)
      %h1 
        =yield(:title)
    - if current_user
      login user : 
      = current_user.name
    %hr/ 
    =render :partial => "application/menu"
    =render :partial => "application/message"
    = yield
view/application/_menu.html.haml
-if @menu_display
  = link_to '新規登録', :controller => 'user', :action => 'new'
  = link_to 'サインアウト', :controller => 'signin', :action => 'signout'
view/application/_message.html.haml
-if @message_display
  - unless flash[:message].blank?
    %div
      %p 
        =flash[:message]
      %hr/ 
/view/signin/index.html.haml
- provide(:title, "BookShelfSystem")
=form_tag("/signin/signin") do
  %p 
    =label_tag 'login', 'login'
    =text_field_tag 'login'
  %p 
    =label_tag 'password', 'Password'
    =password_field_tag 'password'
  %p 
    =submit_tag 'submit'
/view/user/list.html.haml
- provide(:title, "ユーザー一覧")
%hr/ 
%table 
  %tr 
    %th id
    %th name
    %th login
    %th email
    %th created_at
    %th updated_at
  -@users.each do |u|
    %tr 
      = content_tag :td, u.id
      = content_tag :td, u.name
      = content_tag :td, u.login
      = content_tag :td, u.email
      = content_tag :td, show_dat(u.created_at)
      = content_tag :td, show_at(u.updated_at)
application_controller.rb
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :authorize

  def authorize
    return redirect_to :controller => 'signin', :action => 'index' unless session[:user_id]
  end

  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def render_partial(partial_settings)
    @menu_display = true if partial_settings[:menu_display]
    @message_display = true if partial_settings[:message_display]
  end
  
  helper_method :current_user
end
signin_controller.rb
class SigninController < ApplicationController
  skip_filter :authorize, :except=>:index
  skip_filter :authorize, :except=>:signin

  def index
    flash[:message]
    render_partial message_display: true
  end

  def signin
    user = User.find_by name: params[:login]
    unless user && user.authenticate(params[:password])
      flash[:message] = "fail password authentication"
      render_partial message_display: true
      return redirect_to action: 'index'
    end

    session[:user_id] = user.id
    render_partial menu_display: true, message_display: true
    redirect_to controller: 'user',action: 'list'
  end

  def signout
    session[:user_id] = nil
    flash[:message] = "ログアウトしました"
    render_partial message_display: true
    redirect_to :controller => 'signin', :action => 'index'
  end
end

レイアウト適用結果

ログイン

ユーザー一覧