LoginSignup
12
10

More than 5 years have passed since last update.

[Rails]条件によって異なるlayoutを使い分けたいとき

Posted at

たとえばユーザが一般ユーザか管理ユーザかとか、多言語対応してたりしてる時に同一controllerだけど異なるlayoutを呼び出したいときある。そんな時はこうするのがいいみたい。

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
 def determine_layout
   if is_admin?
     "admin"
   else
     "application"
   end
end
app/controllers/some_controller.rb
class ApplicationController < ApplicationController
  layout :determine_layout
  ...
end

参考:http://apidock.com/rails/ActionController/Layout/ClassMethods/layout

12
10
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
12
10