Thursday, August 17, 2017

Rails 5: Autoloading and Reloading Constants

Rails 5 disables autoloading after booting the app in production. With Rails 5, autoloading is now completely disabled if config.eager_load = true.

The concept of loading all the constants even before they are actually needed is called “Eager loading”. In a way it is opposite of “Autoloading”. In the case of “Autoloading” the application does not load the constant until it is needed. Once a class is needed and it is missing then the application starts looking in “autoloading paths” to load the missing class.

When application boots in production then the application loads all constants found in all directories listed in eager_load_paths. If folder in the autoload_paths but not in the eager_load_paths are not going to be loaded in production. What this means is that if file paths outside of app/ are in your config.autoload_paths and not in config.eager_load_paths (for example, lib/), these files will no longer load in deployed environments.

In my case I had a file called calendar.rb in the lib folder.

class Calendar <, :date, :callback)

and I was trying to use this class in a helper:

module CalendarHelper

  include ActionView::Helpers::OutputSafetyHelper

  include ActionView::Helpers::TagHelper

  include ActionView::Context
  def calendar(date = Date.current, &block), date,  block).display


In my application.rb I had next configuration:

config.autoload_paths += %W(#{config.root}/lib)

But when I was trying to call from my helper, I was getting an error. Strange errors regarding missing requires and dependencies.

ActionView::Template::Error (uninitialized constant CalendarHelper::Calendar):

So what I had to do was change the configuration

# config/application.rb
config.eager_load_paths << Rails.root.join('lib')

And now everything works fine

Tuesday, August 08, 2017

How to connect the localhost Rails site via Mobile locally

If you want to test your app locally in a mobile or tablet, without the need to have a server, you can try ngrok.  Ngrok is a very cool, lightweight tool that creates a secure tunnel on your local machine along with a public URL you can use for browsing your local site.

It is very simple to use. Follow these steps:

Step 1

Download ngrok and install it.

Step 2

Run your Rails app:
$ rails s

Step 3

Go to the directory where you extracted the zip file and run it.

$  ./ngrok http 3000

Step 4
You can now access the Rails app from the URL displayed in the ngrok output. It will be something like: