ååã®è¨äºã§æ¸ããã¨ããRailsã®ç°å¢ãæ´æ°ãã¾ããã
ããæ¬çªç°å¢ãããã¢ã»ãããã¤ãã©ã¤ã³ã§ããã£ã¦ãã¾ãã¾ããã
ããã§å¾©ç¿ãã¦ããã¾ãã
ããããã¢ã»ãããã¤ãã©ã¤ã³ã¨ã¯ï¼
ãapp/assetsããlib/assetsããvendor/assetsããªã©ç°ãªããã¹ä¸ã®jsãcssãåä¸ã®urlï¼ä¾ãã°http://myapp:3000/assets/xxxxï¼ã§åç §ãå¯è½ã«ããä»çµã¿ã§ããï¼jsãcssã®çµåã ã£ããå§ç¸®ã ã£ããä»ã«ãè²ã ä»äºãã¦ã¾ããï¼
ã¢ã»ãããã¤ãã©ã¤ã³ã®å¯¾è±¡ã¨ãªããã¹ã¯ãRails.application.assets.pathsãã§åç §ã§ãã¾ãã
#/myappã$RAILS_ROOT $ script/rails console irb(main):001:0> puts Rails.application.assets.paths /myapp/app/assets/images /myapp/app/assets/javascripts /myapp/app/assets/stylesheets /myapp//vendor/assets/javascripts /myapp/vendor/assets/stylesheets /myapp/vendor/bundle/ruby/2.1.0/gems/jquery-rails-3.1.0/vendor/assets/javascripts /myapp/vendor/bundle/ruby/2.1.0/gems/coffee-rails-4.0.1/lib/assets/javascripts
アセットパイプライン(Asset Pipeline) - - Railsドキュメント
5分でわかる!? アセットパイプライン(Assets Pipeline) - Rails つまみぐい
RailsのAsset Pipelineの理解とgemで管理しないJavaScriptライブラリの配置 | EasyRamble
#279 Understanding the Asset Pipeline - RailsCasts
productionç°å¢ã«ãããã¢ã»ãããã¤ãã©ã¤ã³
ãã®ã¢ã»ãããã¤ãã©ã¤ã³ãproductionç°å¢ã§ã¯ãã³ã³ãã¤ã«æ¸ã¿ã®ãã¡ã¤ã«ãæ¢ããã¨ããåããã¨ãã¾ãã
ã¤ã¾ããããã¤åã«ã³ã³ãã¤ã«ããªãã¨ãã¢ã»ãããã¤ãã©ã¤ã³ä¸ã«é
ç½®ãããã¡ã¤ã«ã404ã¨ã©ã¼ã¨ãªã£ã¦ãã¾ãã¾ãã
解決ç
1. æåã§ããªã³ã³ãã¤ã«ãã
rakeã¿ã¹ã¯ã§ã¢ã»ãããã¡ã¤ã«ãããªã³ã³ãã¤ã«ãã¦ãããã¨ãã§ãã¾ãã
$ bundle exec rake assets:precompile RAILS_ENV=production
ããããã¨$RAILS_ROOT/public/assetsã®ä¸ã«ããªã³ã³ãã¤ã«ããããã¡ã¤ã«ãã§ããããã¾ãã
ãããã®æ¹æ³ã¯å½ç¶ãªããããªã³ã³ãã¤ã«ãããã¡ã¤ã«ããªãã¸ããªã«ç»é²ããå¿
è¦ãããã¡ãã£ã¨ã¤ãã¤ãããã
ããã§ã
2.åçã«ã³ã³ãã¤ã«ãã
ãµã¼ãããªã¯ã¨ã¹ããåãåã£ãéãã³ã³ãã¤ã«æ¸ã¿ã®ãã¡ã¤ã«ãè¦ã¤ãããªãå ´åã«ãã®å ´ã§ã³ã³ãã¤ã«ãè¡ãè¨å®ãå¯è½ã§ãã
è¨å®ã¯$RAILS_ROOT/config/environments/production.rbã§è¡ãã¾ãã
#config/environments/production.rb # Don't fallback to assets pipeline if a precompiled asset is missed config.assets.compile = true #trueã«è¨å®ããã¨åçã³ã³ãã¤ã«ããæå¹ã
ãããªããªãã¸ããªã¸ã®ç»é²ã¯ä¸è¦ã§ãããããåçã³ã³ãã¤ã«ãªã®ã§ååãªã¯ã¨ã¹ãæã«ã¯ã¬ã¹ãã³ã¹ãç ç²ã«ãªãã¾ãã
ãããå«ãªãã
3.ãããã¤æã«ããªã³ã³ãã¤ã«ããã
capistrano-railsãå©ç¨ãããã¨ã§capistranoã«ãããããã¤æããããã¤å
ã«ããªã³ã³ãã¤ã«ããã¢ã»ãããã¡ã¤ã«ãä½æãããã¨ãã§ãã¾ãã
Gemfileã«å®ç¾©ãã¦
#Gemfile ⦠gem 'capistrano-rails'
ã¤ã³ã¹ãã¼ã«ãã¦
$ bundle install
Capfileã§èªã¿è¾¼ãã°OKã
#Capfile ⦠require 'capistrano/rails/assets'
ããã§ãããã¤å
ã®$RAILS_ROOT/current/public/assetsã«ã³ã³ãã¤ã«æ¸ã¿ã®ãã¡ã¤ã«ãé
ç½®ããã¾ãã
ãããã¯ãã ã£ããã»ã»ã»
ä¸è¨ã®Capistranoã«ããããªã³ã³ãã¤ã«ãå®æ½ããã®ã§ãããä¸æãããã¾ããã§ããã
ãããªã¨ã©ã¼ãåºãã®ã§ãã
Started GET "/assets/first.png" for 106.188.44.126 at 2014-06-27 00:51:18 +0900 ActionController::RoutingError (No route matches [GET] "/assets/first.png"):
ä¸è¨ã®first.pngã¯$RAILS_ROOT/app/assets/imagesã«æ ¼ç´ãã¦ãã¾ãã
ã¢ã»ãããã¤ãã©ã¤ã³ä¸ã«ç½®ãã¦ããããããªã³ã³ãã¤ã«ãããã®ã«ãã¡ã¤ã«ãè¦ã¤ãããªãï¼
ãã®ã¨ã©ã¼ã®åå ã¯ç´æ¥ãã¹ãæå®ãã¦ãããã¨ã«ããã¾ããã
ä¸è¨ã®ç»åã¯viewã®ä¸ã§ç´æ¥ãã¹ãæå®ãã¦ãã¾ããã
<img serc="assets/first.png" />
developmentãªãã°ç¹ã«åé¡ããã¾ããããproductionç°å¢ã§ã¯ãããpublic/assetsã«ç½®ãå¿
è¦ãããã¾ãã
ããã§ããªã³ã³ãã¤ã«ãå®æ½ããããã§ãããRails4ããã¯ããªã³ã³ãã¤ã«ã®ææç©ãRails3ã®é ã¨å¤æ´ã«ãªã£ã¦ã¾ãã
ä¸è¨first.pngãä¾ã«åãã¨ãRails3ã®é ã¯
public/assets/first.png public/assets/first-xxxxx.png #xxxxxã¯ããã·ã¥å¤
ã¨å ã®ãã¡ã¤ã«åãã®ã¾ã¾ã®ãã®ã¨ããã·ã¥å¤ãä»ãããã¡ã¤ã«ãçæããã¦ããã®ã«å¯¾ããRails4ã§ã¯
public/assets/first-xxxxx.png #xxxxxã¯ããã·ã¥å¤
ã¨ããã·ã¥å¤ä»ãã®ãã®ããçæããã¾ããã
ãã®ããç´æ¥ãã¹ãæå®ãã¦ããã¨æ¸ãã¦ããéãã®ãã¡ã¤ã«åããªã¯ã¨ã¹ãããã404ã¨ãªãã¾ãã
ããã«å¯¾ãã解決æ¹æ³ã¯asset_pathã¡ã½ãããå©ç¨ãããã¨ã§ãã
<img src="<%= asset_path 'first.png' %>" />
asset_pathã«ã¢ã»ãããã¤ãã©ã¤ã³ä¸ã®ãã¡ã¤ã«åãæå®ããã ãã§ãdevelopmentãproductionããããã«é©ããå½¢ã«å¤æãã¦ããã¾ãã
#development <img src="/assets/first.png" />
#production <img src="/assets/first-696d2d60937a6e31851de94c4e646e3b.png" />
Rails4ではbackground:url("assets/hoge.png")の書き方は動かない話
asset_path - リファレンス - - Railsドキュメント
Railsのasset_pathは何をやってくれてるのか - (゚∀゚)o彡 sasata299's blog
ããã§è§£æ±ºï¼ã¨æã£ã¦ãããå¥ã®åé¡ãèµ·ãã¦ãã¾ããã
Started GET "/index.css" for 127.0.0.1 at 2014-06-28 18:11:40 +0900 ActionController::RoutingError (No route matches [GET] "/index.css"):
index.cssã¯ããããã¼ã¸ã§ã®ã¿ä½¿ããããããapplication.cssã§ã¯requireããvendor/assets/stylesheetsã«ç½®ãã¦ãã¾ãã
ãããªã¯ã¨ã¹ãããããã¡ã¤ã«åãè¦ãã¨ãããã·ã¥å¤ãªãã®ãã¡ã¤ã«åãã»ã»ã»
app/public/assetså
ã確èªããã¨ããã®cssã¯ããªã³ã³ãã¤ã«ããã¦ãã¾ããã§ããã
åå ã¯Rails4ã§ã®assets:precompileã«ãããã³ã³ãã¤ã«å¯¾è±¡ã«ããã¾ããã
Rails4ã§ã¯ã¢ã»ãããã¡ã¤ã«ã®ããªã³ã³ãã¤ã«å¯¾è±¡ã以ä¸ã®ããã«å®ç¾©ããã¦ãã¾ãã
@assets.precompile = [ Proc.new { |path, fn| fn =~ /app\/assets/ && !%w(.js .css).include?(File.extname(path)) }, /(?:\/|\\|\A)application\.(css|js)$/ ]
èªã¿è§£ãã¨
- app/assets/ã¨ãããã¹ãå«ã¿
- .jsã.css以å¤ã®è³ç£ ã¾ãã¯
- application.jsãapplication.css
ãããªã³ã³ãã¤ã«ã®å¯¾è±¡ã¨ãªãã¾ãã
ã¤ã¾ããã¢ã»ãããã¤ãã©ã¤ã³ä¸ã®ãã¡ã¤ã«â ããªã³ã³ãã¤ã«å¯¾è±¡ã£ã¦ãã¨ãªã®ï¼
ãããã©ãããã°ããã®ããapplication.cssã«å«ãããããªãã®ãï¼
ã³ã³ãã¤ã«å¯¾è±¡ãå¢ããããå ´åã¯config/application.rbå ã§è¨å®ãããã¨ãå¯è½ã§ãã
#config/application.rb config.assets.precompile += %w(index.css)
Rails4でGemの資産がAssets Precomplieに含まれないときは · THINKING MEGANE
ããã§å
¨ã¦è§£æ±ºãã¾ããã
ã§ããã®æ¹æ³ã ã¨application.js|cssã§requireããªããã¡ã¤ã«ãå¢ãããã³ã«è¨å®ã追å ããªãã¨ãããªãã¦å¾®å¦ã ãªã¼ãã¨ãæãã¾ãã
ãã¼ããRailsé£ããï¼
Ruby on Rails 4 ã¢ããªã±ã¼ã·ã§ã³ããã°ã©ãã³ã°
- ä½è : å±±ç°ç¥¥å¯
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2014/04/11
- ã¡ãã£ã¢: 大åæ¬
- ãã®ååãå«ãããã° (6件) ãè¦ã
å®è·µRuby on Rails 4 ç¾å ´ã®ããããå¦ã¶æ¬æ ¼Webããã°ã©ãã³ã°
- ä½è : é»ç°åª
- åºç社/ã¡ã¼ã«ã¼: ã¤ã³ãã¬ã¹
- çºå£²æ¥: 2014/05/23
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- ãã®ååãå«ãããã° (3件) ãè¦ã
- ä½è : é«æ©å¾ç¾©,å¾è¤è£èµ,ã¾ã¤ãã¨ããã²ã
- åºç社/ã¡ã¼ã«ã¼: ã½ãããã³ã¯ã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2013/06/04
- ã¡ãã£ã¢: åè¡æ¬
- ãã®ååãå«ãããã° (33件) ãè¦ã