16
14

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.

Rails で環境ごとに見た目を変える

Posted at

1 変更したい内容を書いた css を環境ごとに用意する

app/assets/stylesheets/environment/ に↓のようなファイルを環境ごとに用意する。

production.css.scss
.navbar-default {
  background-color: #35fff5;
}

2 intializer の中で環境ごとに読み込み対象を切り替える

config/initializer/assets
environment.rb で環境ごとに切り替える。
#もっといい書き方がありそうな・・

environment.rb
case Rails.env
  when 'development' then
    Rails.application.config.assets.precompile += %w( environment/development.css )
  when 'staging' then
    Rails.application.config.assets.precompile += %w( environment/staging.css )
  when 'production' then
    Rails.application.config.assets.precompile += %w( environment/production.css )
  else
end

3 application.html で環境ごとの css を読み込む

application.html.haml
= stylesheet_link_tag    asset_path "environment/#{Rails.env}", media: 'all', 'data-turbolinks-track' => true

Htmlのどこかにも環境名を出しておくと親切な気もする。

%a.navbar-brand{:href => "/"} Admin #{Rails.env}環境

4 できた

Screenshot 2014-05-22 15.53.41.png

Screenshot 2014-05-22 15.54.06.png

5 注意

application.css.scss で require_tree をしてると全環境読み込まれるので個別に css を指定しないといけない。#view ごとの css にスタイルを書くってあまりないから問題ないような気がしています。

16
14
4

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
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?