如何使用Rails 4和Ruby 2.0应用程序实时重新加载? [英] How to do live reload with Rails 4 and Ruby 2.0 app?

查看:59
本文介绍了如何使用Rails 4和Ruby 2.0应用程序实时重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在Rails 4 + Ruby 2.0项目的文本编辑器中编辑.scss或.erb文件,我希望在浏览器中看到实时变化。

I want to see live changes in browser if I edit .scss or .erb file in text Editor in Rails 4 + Ruby 2.0 project.

我尝试了Guard和Guard-live-reload,但抛出以下错误。

I tried Guard and Guard-live-reload, but it's throwing the following error.

22:58:04 - ERROR - Could not load 'guard/rspec' or find class Guard::Rspec
22:58:04 - ERROR - /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:100:in `require'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:100:in `plugin_class'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:57:in `initialize_plugin'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard.rb:167:in `add_plugin'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:174:in `block in guard'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:173:in `each'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:173:in `guard'
> [#] /home/jitendra/milaap-webapp/Guardfile:4:in `_instance_eval_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:97:in `instance_eval'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:97:in `_instance_eval_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:37:in `evaluate_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/setuper.rb:148:in `evaluate_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/setuper.rb:64:in `setup'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/commander.rb:24:in `start'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/cli.rb:107:in `start'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/bin/guard:6:in `<top (required)>'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/guard:23:in `load'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/guard:23:in `<main>'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/ruby_executable_hooks:15:in `eval'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/ruby_executable_hooks:15:in `<main>'
22:58:04 - ERROR - Invalid Guardfile, original error is:
> [#] undefined method `superclass' for nil:NilClass
/home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:57:in `initialize_plugin': undefined method `superclass' for nil:NilClass (NoMethodError)
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard.rb:167:in `add_plugin'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:174:in `block in guard'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:173:in `each'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:173:in `guard'
    from /home/jitendra/milaap-webapp/Guardfile:4:in `_instance_eval_guardfile'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:97:in `instance_eval'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:97:in `_instance_eval_guardfile'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:37:in `evaluate_guardfile'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/setuper.rb:148:in `evaluate_guardfile'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/setuper.rb:64:in `setup'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/commander.rb:24:in `start'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/cli.rb:107:in `start'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/bin/guard:6:in `<top (required)>'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/guard:23:in `load'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/guard:23:in `<main>'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/ruby_executable_hooks:15:in `eval'
    from /home/jitendra/.rvm/gems/ruby-2.1.1@global/bin/ruby_executable_hooks:15:in `<main>'


推荐答案

这是我使用livereload的方式:

This is how i work with livereload:

1。)获取宝石

在您的Gemfile中:

In your Gemfile:

group :development do
  gem 'guard-livereload', require: false
end

2。) guard init livereload ,它将生成一个 Gu ardfile 放在应用程序的根目录。

2.) guard init livereload, Which will generate a Guardfile at the root of your App.

3。)打开Guardfile,它看起来应该像这样(只是Guard-Livereload,如果您运行其他防护插件,请确保它们在livereload以下。)

3.) Opening your Guardfile it should look like this (Just the Guard-Livereload, if you run other guard plugins make sure they're below the livereload.)

guard 'livereload' do
  watch(%r{app/views/.+\.(erb)$})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{public/.+\.(css|js|html)})
  watch(%r{config/locales/.+\.yml})
  watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
end

4。) Chrome网上应用店中获取Livereload Chrome应用

5。)重新启动服务器并打开一个单独的标签,然后键入-> guard

5.) Restart your server and open a separate tab and type-> guard

6。)。在您的浏览器中,按下livereload按钮,它应该将其链接(在防护标签中的浏览器已连接)

6.) In your Browser push the livereload button and it should link it (Browser Connected in the Guard Tab)

我为其他人撰写了广泛的文章,可能会偶然发现相同的问题。对于您的具体情况,请阅读#3 。打开保护文件,并确保首先调用livereload。

I wrote it extensively for other's which may stumble upon the same question. For your specific case read #3. Open your guardfile and make sure livereload is called first.


观看 Railscast#264 Guard

使用SSL时,livereload不会

When working with SSL, livereload doesn't like that quite well.

Rack-Livereload 是一个简洁的小工具,您可以将其添加到项目中来解决SSL问题。 gem插入了一块或Rack中间件,并基本上连接到livereload应用,以通过现有的(和启用SSL的)Rails服务器提供JavaScript。

Rack-Livereload is a neat little gem which you can add to your project to get around the SSL problems. The gem inserts a piece or Rack middleware and basically connects to the livereload app to serve up the javascript through the existing (and SSL enabled) Rails server.

这篇关于如何使用Rails 4和Ruby 2.0应用程序实时重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆