Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2.11 suddenly needs a restart to 'acknowledge' any change in a controller?

Title says it all.

Note that this is not about a change in the model or initializers. I can delete an instance variable in the controller (say, @user) and then reload a view and it will work - until I restart the server, in which case it will complain about the variable being nil.

I was working normally and then switched to work on a totally different set of controllers and views and now it's happening for no reason whatsoever.

The app is in a development environment.

development.rb contents:

Dashboard::Application.configure do
  config.cache_classes = false
  config.whiny_nils = true
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.action_dispatch.best_standards_support = :builtin
  config.assets.compress = false
  config.assets.debug = true
end

How can I find out how it's happening and how do I fix it?

Edit: **It's likely related, but I can't seem to use any paths that exist when running 'rake routes' in a partial, such as dashboards_path**

Plot twist: Adding

config.reload_classes_only_on_change = false

to development.rb seemed to have ameliorated the issue. I still would like to know why it happened, why it happened out of the blue and why it happened to one controller but not the other.

like image 387
dsp_099 Avatar asked Jan 12 '13 18:01

dsp_099


1 Answers

Rails uses the autoload paths config to determine what files to auto load, and reload:

module YourApp
  class Application < Rails::Application
    config.autoload_paths += %W( #{config.root}/lib #{config.root}/lib/**/ #{config.root}/app/traits )

    ...

  end
end

As you can see I have added a custom directory, the app/traits directory, where I store some modules that define shared behavior.

If the controller you started working on is in a subdirectory that is not watched by rails or has permissions that stop rails from attaching a file system changed event you get this problem.

The reason that config.reload_classes_only_on_change = false "solves" the problem is that the entire app is reloaded on every request instead of relying on detecting changes to files.

Most likely the controller is not in the watched files list and thats why rails is not reloading it on change. The exact reason why it's not on the list might vary and I need more details about the folder structure and config of the app before I can give a good answer there...

like image 191
Jonas Schubert Erlandsson Avatar answered Nov 15 '22 07:11

Jonas Schubert Erlandsson