如何在Rails中启用自动代码重新加载

And*_*rie 6 html css ruby-on-rails

有没有办法在开发环境中使用Rails应用程序进行"热代码重新加载"?

例如:我正在研究Rails应用程序,我在样式表中添加了几行css,我查看浏览器以查看修改后的样式.截至目前,我必须使用cmd-r或单击刷新按钮刷新页面.

有没有办法让页面在更改时自动重新加载?

这在Phoenix Web框架中运行良好(我确信Phoenix不是此功能中唯一的框架).如何在Ruby on Rails中启用这样的功能?

gus*_*ics 8

I am using this setup reloads all assets, js, css, ruby files

in Gemfile

group :development, :test do
  gem 'guard-livereload', '~> 2.5', require: false
end

group :development do
 gem 'listen'
 gem 'guard'
 gem 'guard-zeus'
 gem 'rack-livereload'
end
Run Code Online (Sandbox Code Playgroud)

insert this in your development.rb

config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
Run Code Online (Sandbox Code Playgroud)

i have this in my guard file

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
#  .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
#  $ mkdir config
#  $ mv Guardfile config/
#  $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

guard 'livereload' do
  extensions = {
    css: :css,
    scss: :css,
    sass: :css,
    js: :js,
    coffee: :js,
    html: :html,
    png: :png,
    gif: :gif,
    jpg: :jpg,
    jpeg: :jpeg,
    # less: :less, # uncomment if you want LESS stylesheets done in browser
  }

  rails_view_exts = %w(erb haml slim)

  # file types LiveReload may optimize refresh for
  compiled_exts = extensions.values.uniq
  watch(%r{public/.+\.(#{compiled_exts * '|'})})

  extensions.each do |ext, type|
    watch(%r{
          (?:app|vendor)
          (?:/assets/\w+/(?<path>[^.]+) # path+base without extension
           (?<ext>\.#{ext})) # matching extension (must be first encountered)
          (?:\.\w+|$) # other extensions
          }x) do |m|
      path = m[1]
      "/assets/#{path}.#{type}"
    end
  end

  # file needing a full reload of the page anyway
  watch(%r{app/views/.+\.(#{rails_view_exts * '|'})$})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{config/locales/.+\.yml})
end

guard 'zeus' do
  require 'ostruct'

  rspec = OpenStruct.new
  # rspec.spec_dir = 'spec'
  # rspec.spec = ->(m) { "#{rspec.spec_dir}/#{m}_spec.rb" }
  # rspec.spec_helper = "#{rspec.spec_dir}/spec_helper.rb"

  # matchers
  # rspec.spec_files = /^#{rspec.spec_dir}\/.+_spec\.rb$/

  # Ruby apps
  ruby = OpenStruct.new
  ruby.lib_files = /^(lib\/.+)\.rb$/

  # watch(rspec.spec_files)
  # watch(rspec.spec_helper) { rspec.spec_dir }
  # watch(ruby.lib_files) { |m| rspec.spec.call(m[1]) }

  # Rails example
  rails = OpenStruct.new
  rails.app_files = /^app\/(.+)\.rb$/
  rails.views_n_layouts = /^app\/(.+(?:\.erb|\.haml|\.slim))$/
  rails.controllers = %r{^app/controllers/(.+)_controller\.rb$}

  # watch(rails.app_files) { |m| rspec.spec.call(m[1]) }
  # watch(rails.views_n_layouts) { |m| rspec.spec.call(m[1]) }
  # watch(rails.controllers) do |m|
  #   [
  #     rspec.spec.call("routing/#{m[1]}_routing"),
  #     rspec.spec.call("controllers/#{m[1]}_controller"),
  #     rspec.spec.call("acceptance/#{m[1]}")
  #   ]
  # end
end
Run Code Online (Sandbox Code Playgroud)

I am using zeus instead of spring on this setup.

Run guard

Open localhost:3000 and you are good to go.

This should resolve your question, and have blazing reload times better than browserify.

I commented out guard looking at test directories if you want you can uncomment those lines if your are doing TDD.


Alb*_*aul 1

当您更改 js 元素(不是 css 或 ruby​​ 文件)时,此 gem 会自动重新加载。

https://github.com/rmosolgo/react-rails-hot-loader

从未见过 Rails 平台中的 CSS 热代码重载。

  • 就这样 - 这对于在 Rails 中自动重新加载 JavaScript 来说非常好。我正在寻找至少能够自动重新加载整个资产管道的东西(JS 和 CSS - 自动重新加载模型、控制器和视图也很好)。 (2认同)