2016-03-12 65 views
11

我注意到我的Rails服務器在我更改它們之後不會重新加載控制器,模型和其他任何文件。我使用Vagrant和Rails API,並且我發現有些人通過在Vagrantfile的下面添加行來解決這個問題。Rails服務器沒有看到代碼更改和重新加載文件

config.vm.provider "virtualbox" do |vb| 
    vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 5000 ] 
end 

它不能解決我的問題。我想盡辦法解決這個問題。我附上可能對您有用的文件。

Gemfile看起來是這樣的:

source 'https://rubygems.org' 

gem 'rake', '< 11.0' 

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '>= 5.0.0.beta3', '< 5.1' 
# Use mysql as the database for Active Record 
# gem 'mysql2', '>= 0.3.18', '< 0.5' 

# User PostgreSQL as the database for Active Record 
gem 'pg', '~> 0.18' 

gem 'active_model_serializers' 

gem 'rspec-its' 

gem 'database_cleaner' 

# Use Puma as the app server 
gem 'puma' 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
# gem 'jbuilder', '~> 2.0' 
# Use Redis adapter to run Action Cable in production 
# gem 'redis', '~> 3.0' 
# Use ActiveModel has_secure_password 
# gem 'bcrypt', '~> 3.1.7' 

# Gem allowing using external APIs 
gem 'httparty' 

# Auth0 gem for authentication using JWT 
gem 'knock' 

gem 'jwt' 

# OpenID Omniauth gem for authenticating Steam users 
gem 'omniauth-steam' 

# Gem for managing environment variables 
gem 'figaro' 

# Use Capistrano for deployment 
# gem 'capistrano-rails', group: :development 

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible 
gem 'rack-cors', :require => 'rack/cors' 

group :development, :test do 
    # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
    gem 'byebug' 
    gem 'rspec-rails', '~> 3.0' 
    gem 'factory_girl_rails' 
    gem 'ffaker' 
end 

group :test do 
    gem 'shoulda-matchers' 
    gem 'json-schema' 
end 

group :development do 
    gem 'listen', '~> 2.10' 
    # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 
    gem 'spring' 
    gem 'spring-watcher-listen', '~> 2.0.0' 
end 

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 

我敢肯定,我運行,因爲日誌

=> Booting Puma 
=> Rails 5.0.0.beta3 application starting in development on http://0.0.0.0:3000 
=> Run `rails server -h` for more startup options 
=> Ctrl-C to shutdown server 
Puma starting in single mode... 
* Version 3.1.0 (ruby 2.2.3-p173), codename: El Niño Winter Wonderland 
* Min threads: 5, max threads: 5 
* Environment: development 
* Listening on tcp://0.0.0.0:3000 

的beggining我在開發模式下的服務器,這是我development.rb文件

Rails.application.configure do 
    # Settings specified here will take precedence over those in config/application.rb. 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the web server when you make code changes. 
    config.cache_classes = false 

    # Do not eager load code on boot. 
    config.eager_load = false 

    # Show full error reports. 
    config.consider_all_requests_local = true 

    # Enable/disable caching. By default caching is disabled. 
    if Rails.root.join('tmp/caching-dev.txt').exist? 
    config.action_controller.perform_caching = true 

    config.action_mailer.perform_caching = false 

    config.cache_store = :memory_store 
    config.public_file_server.headers = { 
     'Cache-Control' => 'public, max-age=172800' 
    } 
    else 
    config.action_controller.perform_caching = false 

    config.action_mailer.perform_caching = false 

    config.cache_store = :null_store 
    end 

    # Don't care if the mailer can't send. 
    config.action_mailer.raise_delivery_errors = false 

    # Print deprecation notices to the Rails logger. 
    config.active_support.deprecation = :log 

    # Raise an error on page load if there are pending migrations. 
    config.active_record.migration_error = :page_load 


    # Raises error for missing translations 
    # config.action_view.raise_on_missing_translations = true 

    # Use an evented file watcher to asynchronously detect changes in source code, 
    # routes, locales, etc. This feature depends on the listen gem. 
    config.file_watcher = ActiveSupport::EventedFileUpdateChecker 
end 

我的Vagrantfile

Vagrant.configure(2) do |config| 

    if Vagrant.has_plugin?("vagrant-timezone") 
    config.timezone.value = "Europe/Warsaw" 
    end 

    config.vm.box = "ubuntu/trusty64" 

    config.vm.network :forwarded_port, guest: 3000, host: 3000 
    config.vm.synced_folder "E:/Projekty - Rails", "/home/projekty" 

    config.vm.provider "virtualbox" do |vb| 
    vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 5000 ] 
    end 
end 
+0

在development.rb文件,你可以添加config.threadsafe!線。請檢查它是否工作? –

+0

@MohammadShahadatHossain當我嘗試啓動服務器時,它會導致錯誤。我使用rails api,所以這可能是爲什麼,對吧? – mparkitny

+0

使用rails api應該不成問題。可能會有一些配置丟失,這就是問題出現的原因。 –

回答

10

我已經解決了我的問題,在development.rb文件中添加了下面的行。

config.reload_classes_only_on_change = false 
+0

由於某種原因,我的彪馬服務器非常慢,並開始打破我的一些設計用戶會話路線。奇怪的。將再試一次,看看它是否仍然發生。 –

+0

是的,它給了我一個'讀取錯誤:#問題。不知道這是由於改變還是其他我做錯的事情的副作用造成的。 –

+0

@JayKilleen我沒有在我的項目中使用Devise,所以我沒有遇到這種類型的錯誤。經過快速研究,我找到了解決您的問題的可能方案你可以閱讀更多[鏈接](http://stackoverflow.com/a/27990374/3648015)並試一試。 – mparkitny

0

我有同樣的問題,所以我做了這樣一個快速的腳本。確保您首先位於您的應用程序文件夾中。

$ sudo的納米凹凸

#/斌/慶典

耙分貝:遷移 回聲 「遷移」 耙路線 回聲 「全軍覆沒」 須藤服務的Apache2重啓 回聲「Web服務器重裝「

CTL + X,Y,輸入

須藤使用chmod + X凸點

現在您只需鍵入./bump,它將運行所有三個命令,然後您知道所有內容都已加載。我也使用這種方法重複這一點,就像命令行安裝諸如設計之類的寶石一樣。

46

It doesn't fix the problem for me.

添加到config/environments/development.rb

#config.file_watcher = ActiveSupport::EventedFileUpdateChecker 
config.file_watcher = ActiveSupport::FileUpdateChecker 

FileUpdateChecker將通過輪詢檢測文件的變化。

+0

在我的development.rb中發生變化,並且像魅力一樣工作 - 謝謝 – Hannes

+0

也爲我工作。我已經受了2個多星期的考慮,認爲這是Rails 5 beta的一個bug。謝謝! – Abdulaziz

+3

嗨,Rev3rse。使用Virtual Box的共享文件夾,發生此問題。由於操作系統(在運行虛擬機)不能檢測共享文件夾中的文件更改事件。 – pocari

0

pocar​​i的解決方案爲我工作,但我必須等待幾秒鐘才能重新加載頁面,否則內容不會一直更新。

添加在this answer描述一個選項synced_folder工作得很好:

config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['actimeo=1'] 

(和development.rb的變化是沒有必要的)

相關問題