Restart Rails Server Automatically After Every Change in Controllers

Rails 5 restart server every time controller or model changes

I've resolved the problem, I stumbled upon this post Vagrant shared folder with rails server which pointed me to the right direction.

Basicallly besides having:

config.cache_classes = false

in the development.rb file it is also necessary to have:

config.reload_classes_only_on_change = false

How to auto restart rails server with grunt?

Yes, of course. You will need to use Guard for this purpose. Please read carefully Guards official page. They're doing good job in explaining how to setup your development environment.

There are tutorials on RailsCasts and also on NetTuts.

Edit: I forgot to mention that there is second(but definitely not the last one) option to achieve this. If you start using Spring which is Rails application preloader. Read more about this on Github page.

Here is great tutorial on how to setup everything together - http://girders.org/blog/2014/02/06/setup-rails-41-spring-rspec-and-guard/

When do I need to restart server in Rails?

2021 UPDATE:

Since this answer is my most visible answer on StackOverflow and it's quite old, I thought it was about time to update it to go a little more in-depth. It's original info is not incorrect, but it's a bit too generic, and I feel I can explain it better with the knowledge I have now. The original answer is preserved below.

The way Rails works vs plain ruby is that it basically requires your files on-the-fly without you needing to add requires on the top of the file. There is nowadays a good relatively in-depth page about it on the official Rails guides here.

Starting on Rails 6, they introduced a new loader to manage the logic for automatically loading the source files, off-loading that work to a gem called zeitwerk. There is also a page on the Rails guides that explains how the new loader works.

The basic idea is something like this:

  1. Rails runs a bunch of config files (environment.rb, application.rb, boot.rb, the respective environments/<environment>.rb, config/routes.rb, initializers, etc). All the files ran in this starting phase are in the config directory, so if the changed file is inside the config directory, it probably will need a Rails restart.

  2. Rails then starts watching all the files under each of it's autoload_paths (by default each dir inside app) to check when any of them change. It also specifically watches for changes on the config/routes.rb file.

  3. Now armed with a nice route configuration, Rails knows to which paths to respond and with which controllers. Once you hit one of the configured routes, Rails will run your controller action.

  4. Whenever ruby sees a constant that it doesn't recognize, it calls the method const_missing. Rails overrides this method to get the name of your constant and use it to search for a file with the same name inside each directory of the autoload_paths.

  5. When it finds the file it then requires it on-the-fly, with the assumption that it will define the constant that triggered the const_missing, and then the code goes on.

  6. Next time the same constant is used, it will now be defined, so it won't even reach the const_missing method.

  7. Last but not least, IF the config cache_classes is set to false (the default on the development environment), then whenever Rails catches a change in one of the files it's watching, it will unset the constant associated with the file (that it knows from the name of the file).

So whenever you need to change anything that is loaded in that step 1, with the exception of the specially treated config/routes.rb, you need to restart Rails. Anything else, Rails will reload by that autoload mechanism unless it's set to cache the result.

In production it is also configured by default to preload the classes, so it will load all the classes before even starting the server. That's to avoid the overhead of the whole const_missing, file searching and dynamic require thing.

Non-ruby files, like assets and views are all read by Rails at the time of request, so you can always change them without restarting Rails. (Note: on production assets are usually pre-compiled, so changing the assets inside app/assets won't result in any change. But it is still loading at the time of request, it's just that the file in question is a compiled bundle inside the public directory).



Original answer:

You need to restart you server when you need Rails to be loaded again from the start.

If you're adding or removing gems, then yes, you will need to restart the server.

If you change your version of ruby, change your Gemfile or change something from internal classes of Rails, you will need to restart it, otherwise it should be ok. But if unexpected problems arise, restart the server is the first thing you should try.

Also, on a side-note, you will only see the changes just refreshing the page if config.cache_classes is set to false (which I think is the default for development, but not for production).

Edit:

Just to make sure everyone will notice, tadman said one wise thing at the comments, The general rule of thumb here is making changes to anything outside of app/ or config/routes.rb or db/ will require a restart.

Automatically restarting a Rails app and running Rake tasks

  1. Just create a Cron task that runs periodically. That Cron task starts a shell stript that just does all the step you would run manually.

  2. There is a gem (https://github.com/biola/turnout) at helps with the maintainance page. It provides rake tasks like rake maintenance:start and rake maintenance:end.

  3. I think it is not necessary to drop the tables. Usually it should be enough to just delete all records and then create new records. If you really have to drop the database, it might be faster to just restore the database schema from a structure dump.

Rails Application expecting me to restart my webrick server for any change in my controller

What environment are you using?

The default environment is 'development', where the code is reloaded on each request. However, this behaviour can be overwritten in the config file.

To be sure that the code is reloaded, add this into your config/development.rb file:

  # 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 webserver when you make code changes.
config.cache_classes = false

Rails Server requiring restart to show changes

I'll bet your Rails version is 3.2.4? Just update your Rails version to 3.2.5+ and you'll be fine.

It's a regression bug in Rails that happened in 3.2.4. It's been fixed: https://github.com/rails/rails/commit/7056079761eba049bbc6108946a1626fe169dbdf



Related Topics



Leave a reply



Submit