Webpacker
Rails 6 版本開始依賴 Webpacker,在運行之前必須先安裝 Webpacker 這玩意。
1rails webpacker:install
如果需要安裝前端框架,請使用 yarn 來安裝,這樣部署的時候能享受到 webpacker 打包便利。
production
Rails 6 啟動時需要一串 Key 作為加密的 salt,key 不能隨意生成。 生成 key 時,請刪除 config 下的 credentials.enc.yml 和 master.key 文件。 然後運行
1rails credentials:edit
然後 Rails 訪問靜態資源,需要使用 webpacker 打包編譯後的資產。 運行
1rails assets:precompile
Rails 6 在生產環境下認為你使用 Apache 和 Nginx 緩存編譯後的靜態資產。如果你不使用他們,需要
1# config/environments/production.rb
2config.public_file_server.enabled = true
記住,打包之後的 js 以及 css 統一叫 application.js/css 在 view 頁面引用時需要引用 application 這個名字。其他的會報錯