首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Rails中启用自动代码重载

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

Stack Overflow用户
提问于 2016-02-19 02:23:17
回答 3查看 13.5K关注 0票数 12

有没有办法在开发环境中对Rails应用程序进行“热代码重载”?

例如:我正在开发一个Rails应用程序,我在样式表中添加了几行css,我查看浏览器以查看修改后的样式。到目前为止,我必须使用cmd-r或单击刷新按钮来刷新页面。

有没有办法让页面在发生变化时自动重新加载?

这在Phoenix web框架中工作得很好(我确信Phoenix不是这个特性中唯一的框架)。如何在Ruby on Rails中启用这样的功能?

EN

回答 3

Stack Overflow用户

发布于 2017-03-31 02:36:06

我正在使用这个设置重新加载所有的资源,js,css,ruby文件

在Gemfile中

代码语言:javascript
复制
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

在你的development.rb中插入这个

代码语言:javascript
复制
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload

我的守卫档案里有这个

代码语言:javascript
复制
# 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

在这个设置中,我使用zeus而不是spring。

运行guard

打开localhost:3000就可以了。

这应该会解决你的问题,并且比browserify有更好的重新加载时间。

我注释掉了查看测试目录的保护,如果你愿意,如果你正在做TDD,你可以取消对这些代码行的注释。

票数 9
EN

Stack Overflow用户

发布于 2016-10-17 14:09:19

当HTML/JS发生变化时,可以通过guard结合livereload来实现CSS热插拔和自动重载:https://github.com/guard/guard-livereload

票数 4
EN

Stack Overflow用户

发布于 2016-02-19 02:27:06

当你改变js元素(不是css或ruby文件)时,这个gem会自动重新加载。

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

从未见过在rails平台中重载css热门代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35489477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档