Howto for the Proxmox live migration

Guides.rubyonrails.org

1 Locations for Initialization Code

Rails offers four standard spots to place initialization code:

  • Environment-specific configuration files
  • Initializers
  • After-initializers

2 Running Code Before Rails

In the rare event that your application needs to run some code before Rails itself is loaded, put it above the call to in.

3 Configuring Rails Components

In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file and environment-specific configuration files (such as) allow you to specify the various settings that you want to pass down to all of the components.

For example, you could add this setting to file:

This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same object in:

Rails will use that particular setting to configure Active Record.

3.1 Rails General Configuration

These configuration methods are to be called on a object, such as a subclass of or.

  • takes a block which will be run nach Rails has finished initializing the application. That includes the initialization of the framework itself, engines, and all the application's initializers in. Note that this block want be run for rake tasks. Useful for configuring values ​​set up by other initializers:

  • sets the host for the assets. Useful when CDNs are used for hosting assets, or when you want to work around the concurrency constraints built-in in browsers using different domain aliases. Shorter version of.

  • accepts an array of paths from which Rails will autoload constants that won't be wiped per request. Relevant if is, which is the default in the development environment. Otherwise, all autoloading happens only once. All elements of this array must also be in. Default is an empty array.

  • accepts an array of paths from which Rails will autoload constants. Default is an empty array. Since Rails 6, it is not recommended to adjust this. See Autoloading and Reloading Constants.

  • says whether autoload paths have to be added to. This flag is by default, but it is recommended to be set to in mode early, in. Zeitwerk uses absolute paths internally, and applications running in mode do not need, so models, controllers, jobs, etc. do not need to be in. Setting this to saves Ruby from checking these directories when resolving calls with relative paths, and saves Bootsnap work and RAM, since it does not need to build an index for them.

  • controls whether or not application classes and modules should be reloaded if they change. Defaults to in the development environment, and in production. In the test environment, the default is if Spring is installed, otherwise.

  • sets the default beginning of week for the application. Accepts a valid day of week as a symbol (e.g.).

  • configures which cache store to use for Rails caching. Options include one of the symbols,,,,, or an object that implements the cache API. Defaults to. See Cache Stores for per-store configuration options.

  • specifies whether or not to use ANSI color codes when logging information. Defaults to.

  • is a flag. If then any error will cause detailed debugging information to be dumped in the HTTP response, and the controller will show the application runtime context in. by default in the development and test environments, and in production. For finer-grained control, set this to and implement in controllers to specify which requests should provide debugging information on errors.

  • allows you to set class that will be used as console you run. It's best to run it in block:

  • controls whether or not someone can start a console in sandbox mode. This is helpful to avoid a long running session of sandbox console, that could lead a database server to run out of memory. Defaults to false.

  • when, eager loads all registered. This includes your application, engines, Rails frameworks, and any other registered namespace.

  • registers namespaces that are eager loaded when is. All namespaces in the list must respond to the method.

  • accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the directory of the application.

  • : when true, enables autoloading, even if the application is eager loaded and is set as true. Defaults to false.

  • sets up the application-wide encoding. Defaults to UTF-8.

  • sets the exceptions application invoked by the ShowException middleware when an exception happens. Defaults to.

  • sets the format used in responses when errors occur in the development environment. Defaults to for API only apps and for normal apps.

  • is the class used to detect file updates in the file system when is. Rails ships with, the default, and (this one depends on the listen gem). Custom classes must conform to the API.

  • used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers. It also filters out sensitive values ​​of database columns when call on an Active Record object. By default, Rails filters out passwords by adding in. Parameters filter works by partial matching regular expression.

  • forces all requests to be served over HTTPS, and sets "https: //" as the default protocol when generating URLs. Enforcement of HTTPS is handled by the middleware, which can be configured via - see its documentation for details.

  • sets the path where your app's JavaScript lives relative to the directory. The default is used by webpacker. An app's configured will be excluded from.

  • defines the formatter of the rails logger. This option defaults to an instance of for all environments. If you are setting a value for you must manually pass the value of your formatter to your logger before it is wrapped in an instance, Rails will not do it for you.

  • defines the verbosity of the Rails logger. This option defaults to for all environments except production, where it defaults to. The available log levels are:,,,,, and.

  • accepts a list of: methods that the object responds to, a that accepts the object, or something that responds to. This makes it easy to tag log lines with debug information like subdomain and request id - both very helpful in debugging multi-user production applications.

  • is the logger that will be used for and any related Rails logging such as. It defaults to an instance of that wraps an instance of which outputs a log to the directory. You can supply a custom logger, to get full compatibility you must follow these guidelines:

    • To support a formatter, you must manually assign a formatter from the value to the logger.
    • To support tagged logs, the log instance must be wrapped with.
    • To support silencing, the logger must include module. The class already includes these modules.
  • allows you to configure the application's middleware. This is covered in depth in the Configuring Middleware section below.

  • when, eager load the application when running Rake tasks. Defaults to.

  • enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to. If is, this option is ignored.

  • configures lookup path for encrypted credentials.

  • configures lookup path for encryption key.

  • is used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get a random generated key in test and development environments, other environments should set one in.

  • Configures Rails to serve static files from the public directory. This option defaults to, but in the production environment it is set to because the server software (e.g. NGINX or Apache) used to run the application should serve static files instead. If you are running or testing your app in production using WEBrick (it is not recommended to use WEBrick in production) set the option to. Otherwise, you won't be able to use page caching and request for files that exist under the public directory.

  • specifies what class to use to store the session. Possible values ​​are which is the default,, and. The last one tells Rails not to deal with sessions. Defaults to a cookie store with application name as the session key. Custom session stores can also be specified:

    This custom store must be defined as.

  • sets the default time zone for the application and enables time zone awareness for Active Record.

  • sets the autoloading mode. This option defaults to when is called with or greater. Applications can still use the classic autoloader by setting this value to after loading the framework defaults:

3.2 Configuring Assets

  • a flag that controls whether the asset pipeline is enabled. It is set to by default.

  • defines the CSS compressor to use. It is set by default by. The unique alternative value at the moment is, which uses the gem.

  • defines the JavaScript compressor to use. Possible values ​​are, and which require the use of the, or gems respectively.

  • a flag that enables the creation of gzipped version of compiled assets, along with non-gzipped assets. Set to by default.

  • contains the paths which are used to look for assets. Appending paths to this configuration option will cause those paths to be used in the search for assets.

  • allows you to specify additional assets (other than and) which are to be precompiled when is run.

  • allows you to modify the behavior of the asset pipeline when an asset is not in the pipeline, if you use sprockets-rails 3.2.0 or newer. Defaults to.

  • defines the prefix where assets are served from. Defaults to.

  • defines the full path to be used for the asset precompiler's manifest file. Defaults to a file named in the directory within the public folder.

  • enables the use of SHA256 fingerprints in asset names. Set to by default.

  • disables the concatenation and compression of assets. Set to by default in.

  • is an option string that is used in SHA256 hash generation. This can be changed to force all files to be recompiled.

  • is a boolean that can be used to turn on live Sprockets compilation in production.

  • accepts a logger conforming to the interface of Log4r or the default Ruby class. Defaults to the same configured at. Setting to will turn off served assets logging.

  • disables logging of assets requests. Set to by default in.

3.3 Configuring Generators

Rails allows you to alter what generators are used with the method. This method takes a block:

The full set of methods that can be used in this block are as follows:

  • allows to create assets on generating a scaffold. Defaults to.
  • allows pluralized model names. Defaults to.
  • defines whether or not to generate helpers. Defaults to.
  • defines which integration tool to use to generate integration tests. Defaults to.
  • defines which integration tool to use to generate system tests. Defaults to.
  • defines which orm to use. Defaults to and will use Active Record by default.
  • defines which generator to use for generating a controller when using. Defaults to.
  • defines whether a resource route definition should be generated or not. Defaults to.
  • different from, defines which generator to use for generating a scaffolded controller when using. Defaults to.
  • turns on the hook for stylesheets in generators. Used in Rails for when the generator is run, but this hook can be used in other generates as well. Defaults to.
  • configures the stylesheet engine (for e.g. sass) to be used when generating assets. Defaults to.
  • creates when generating a scaffolded resource. Defaults to.
  • defines which test framework to use. Defaults to and will use minitest by default.
  • defines which template engine to use, such as ERB or Haml. Defaults to.

3.4 Configuring middleware

Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:

  • Against DNS rebinding and prevents other header attacks. It is included in the development environment by default with the following configuration:

    In other environments is empty and no header checks will be done. If you want to guard against header attacks on production, you have to manually permit the allowed hosts with:

    The host of a request is checked against the entries with the case operator (), which lets support entries of type, and to name a few. Here is an example with a regexp.

    The provided regexp will be wrapped with both anchors (and) so it must match the entire hostname. For example, once anchored, would fail to match.

    A special case is supported that allows you to permit all sub-domains:

  • forces every request to be served using HTTPS. Enabled if is set to. Options passed to this can be configured by setting.

  • is used to serve static assets. Disabled if is. Set if you need to serve a static directory index file that is not named. For example, to serve instead of for directory requests, set to.

  • allows thread safe code reloading. Disabled if is, which causes to be loaded. wraps the app in mutex so it can only be called by a single thread at a time.

  • serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.

  • sets an header containing the time (in seconds) taken to execute the request.

  • notifies the logs that the request has begun. After request is complete, flushes all the logs.

  • rescues any exception returned by the application and renders nice exception pages if the request is local or if is set to. If is set to, exceptions will be raised regardless.

  • makes a unique X-Request-Id header available to the response and enables the method. Configurable with.

  • checks for IP spoofing attacks and gets valid from request headers. Configurable with the, and options.

  • intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header. Configurable with.

  • runs the prepare callbacks before serving the request.

  • sets cookies for the request.

  • is responsible for storing the session in cookies. An alternate middleware can be used for this by changing the to an alternate value. Additionally, options passed to this can be configured by using.

  • sets up the keys. Only available if is set to a value.

  • allows the method to be overridden if is set. This is the middleware which supports the PATCH, PUT, and DELETE HTTP method types.

  • converts HEAD requests to GET requests and serves them as so.

Besides these usual middleware, you can add your own by using the method:

This will put the middleware on the end of the stack. You can use if you wish to add a middleware before another.

Or you can insert a middleware to exact position by using indexes. For example, if you want to insert middleware on top of the stack, you can do it, like so:

There's also which will insert a middleware after another:

Middlewares can also be completely swapped out and replaced with others:

Middleware can be moved from one place to another:

This will move the middleware before. You can also move it after:

They can also be removed from the stack completely:

3.5 Configuring i18n

All these configuration options are delegated to the library.

  • defines the permitted available locales for the app. Defaults to all locale keys found in locale files, usually only on a new application.

  • sets the default locale of an application used for i18n. Defaults to.

  • Ensures that all locales passed through i18n must be declared in the list, raising an exception when setting an unavailable locale. Defaults to. It is recommended not to disable this option unless strongly required, since this works as a security measure against setting any invalid locale from user input.

  • sets the path Rails uses to look for locale files. Defaults to.

  • determines whether an error should be raised for missing translations in controllers and views. These defaults to.

  • sets fallback behavior for missing translations. Here are 3 usage examples for this option:

    • You can set the option to for using default locale as fallback, like so:
    • Or you can set an array of locales as fallback, like so:
    • Or you can set different fallbacks for locales individually. For example, if you want to use for and, for as fallbacks, you can do it, like so:

3.6 Configuring Active Model

  • is a boolean value which controls whether the error format can be overridden at the attribute or model level in the locale files. This is by default.

3.7 Configuring Active Record

includes a variety of configuration options:

  • accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling on either an Active Record model class or an Active Record model instance. Set to disable logging.

  • lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named (and this configuration option doesn't need to be set). There are two other choices:

    • would make the primary key for the customer class.
    • would make the primary key for the customer class.
  • lets you set a global string to be prepended to table names. If you set this to, then the customer class will look for as its table. The default is an empty string.

  • lets you set a global string to be appended to table names. If you set this to, then the customer class will look for as its table. The default is an empty string.

  • lets you set a string to be used as the name of the schema migrations table.

  • lets you set a string to be used as the name of the internal metadata table.

  • lets you set an array of names of environments where destructive actions should be prohibited.

  • specifies whether Rails will look for singular or plural table names in the database. If set to (the default), then the customer class will use the table. If set to false, then the customer class will use the table.

  • determines whether to use (if set to) or (if set to) when pulling dates and times from the database. The default is.

  • controls the format for dumping the database schema to a file. The options are (the default) for a database-independent version that depends on migrations, or for a set of (potentially database-dependent) SQL statements.

  • specifies if an error should be raised if the order of a query is ignored during a batch query. The options are (raise error) or (warn). Default is.

  • controls whether migrations are numbered with serial integers or with timestamps. The default is to use timestamps, which are preferred if there are multiple developers working on the same application.

  • controls whether Active Record will use optimistic locking and is by default.

  • controls the format of the timestamp value in the cache key. Default is.

  • is a boolean value which controls whether or not timestamping of and operations on a model occur. The default value is.

  • is a boolean value and controls whether or not partial writes are used (i.e. whether updates only set attributes that are dirty). Note that when using partial writes, you should also use optimistic locking since concurrent updates may write attributes based on a possibly stale read state. The default value is.

  • is a boolean value which controls whether Active Record should try to keep your test database schema up-to-date with (or) when you run your tests. The default is.

  • is a flag which controls whether or not schema dump should happen (or) when you run migrations. This is set to in which is generated by Rails. The default value is if this configuration is not set.

  • controls which database schemas will be dumped when calling. The options are (the default) which dumps any schemas listed in, which always dumps all schemas regardless of the, or a string of comma separated schemas.

  • is a boolean value and controls whether a record fails validation if association is not present.

  • enables raising or logging an exception if strict_loading is set on an association. The default value is in all environments. It can be changed to send violations to the logger instead of raising.

  • is a boolean value that either enables or disables strict_loading mode by default. Defaults to.

  • allows setting a warning threshold for query result size. If the number of records returned by a query exceeds the threshold, a warning is logged. This can be used to identify queries which might be causing a memory bloat.

  • allows errors for nested relationships to be displayed with an index as well as the error. Defaults to.

  • enables users to get schema cache information from (generated by), instead of having to send a query to the database to get this information. Defaults to.

  • indicates whether to use a stable method that is accompanied by a changing version in the method.

  • enables the same cache key to be reused when the object being cached of type changes by moving the volatile information (max updated at and count) of the relation's cache key into the cache version to support recycling cache key.

  • enables setting the inverse record when traversing to associations.

  • allows to enable new connection handling API. For applications using multiple databases, this new API provides support for granular connection swapping.

  • allows specifying the job that will be used to destroy the associated records in background. It defaults to.

  • allows specifying the Active Job queue to use for destroy jobs. When this option is, purge jobs are sent to the default Active Job queue (see). It defaults to.

The MySQL adapter adds one additional configuration option:

  • controls whether Active Record will consider all columns as booleans. Defaults to.

The PostgreSQL adapter adds one additional configuration option:

  • controls whether database tables created should be "unlogged," which can speed up performance but adds a risk of data loss if the database crashes. It is highly recommended that you do not enable this in a production environment. Defaults to in all environments.

The schema dumper adds two additional configuration options:

  • accepts an array of tables that should not be included in any generated schema file.

  • allows setting a different regular expression that will be used to decide whether a foreign key's name should be dumped to db / schema.rb or not. By default, foreign key names starting with are not exported to the database schema dump. Defaults to.

3.8 Configuring Action Controller

includes a number of configuration settings:

  • sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself. You should only use this if you have a different configuration for Action Mailer, otherwise use.

  • configures whether the application should perform the caching features provided by the Action Controller component or not. Set to in the development environment, in production. If it's not specified, the default will be.

  • configures the extension used for cached pages. Defaults to.

  • configures whether all view helpers are available everywhere or are scoped to the corresponding controller. If set to, methods are only available for views rendered as part of. If, methods are available everywhere. The default configuration behavior (when this option is not explicitly set to or) is that all view helpers are available to each controller.

  • accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action Controller. Set to disable logging.

  • sets the token parameter name for RequestForgery. Calling sets it to by default.

  • enables or disables CSRF protection. By default this is in the test environment and in all other environments.

  • configures whether the HTTP header should be checked against the site's origin as an additional CSRF defense.

  • configures whether CSRF tokens are only valid for the method / action they were generated for.

  • determines whether forgery protection is added on.

  • configures whether generated CSRF tokens are URL-safe.

  • can be used to tell rails that you are deploying to a subdirectory. The default is.

  • sets all the parameters for mass assignment to be permitted by default. The default value is.

  • enables logging or raising an exception if parameters that are not explicitly permitted are found. Set to or to enable. The default value is in development and test environments, and in all other environments.

  • sets a list of permitted parameters that are permitted by default. The default values ​​are.

  • determines whether to log fragment cache reads and writes in verbose format as follows:

    By default it is set to which results in the following output:

3.9 Configuring Action Dispatch

  • sets the name of the store for session data. The default is; other valid options include, or the name of your own custom class.

  • is a hash with HTTP headers that are set by default in each response. By default, this is defined as:

  • specifies the default character set for all renders. Defaults to.

  • sets the TLD (top-level domain) length for the application. Defaults to.

  • is used to determine whether to ignore accept headers from a request. Defaults to.

  • specifies server specific X-Sendfile header. This is useful for accelerated file sending from server. For example it can be set to 'X-Sendfile' for Apache.

  • sets the HTTP Auth salt value. Defaults to.

  • sets the signed cookies salt value. Defaults to.

  • sets the encrypted cookies salt value. Defaults to.

  • sets the signed encrypted cookies salt value. Defaults to.

  • sets the authenticated encrypted cookie salt. Defaults to.

  • sets the cipher to be used for encrypted cookies. These defaults to.

  • sets the digest to be used for signed cookies. These defaults to.

  • allows rotating secrets, ciphers, and digests for encrypted and signed cookies.

  • controls whether signed and encrypted cookies use the AES-256-GCM cipher or the older AES-256-CBC cipher. It defaults to.

  • enables writing cookies with the purpose and expiry metadata embedded. It defaults to.

  • configures whether the method should be performed on the parameters. See Security Guide for more information. It defaults to.

  • configures what exceptions are assigned to an HTTP status. It accepts a hash and you can specify pairs of exception / status. By default, this is defined as:

    Any exceptions that are not configured will be mapped to 500 Internal Server Error.

  • configures the default value of the attribute when setting cookies. When set to, the attribute is not added. To allow the value of the attribute to be configured dynamically based on the request, a proc may be specified. For example:

  • configures the default HTTP status code used when redirecting non-GET / HEAD requests from HTTP to HTTPS in the middleware. Defaults to as defined in https://tools.ietf.org/html/rfc7538.

  • takes a block of code to run before the request.

  • takes a block of code to run after the request.

3.10 Configuring Action View

includes a small number of configuration settings:

  • controls whether or not templates should be reloaded on each request. Defaults to whatever is set for.

  • provides an HTML generator for displaying errors that come from Active Model. The default is

  • tells Rails which form builder to use by default. The default is. If you want your form builder class to be loaded after initialization (so it's reloaded on each request in development), you can pass it as a.

  • accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action View. Set to disable logging.

  • gives the trim mode to be used by ERB. It defaults to, which turns on trimming of tail spaces and newline when using or. See the Erubis documentation for more information.

  • allows you to set the default behavior for in forms with. By default it's set to, which means that remote forms will not include, which is helpful when you're fragment-caching the form. Remote forms get the authenticity from the tag, so embedding is unnecessary unless you support browsers without JavaScript. In such case you can either pass as a form option or set this config setting to.

  • determines whether or not partials are looked up from a subdirectory in templates rendered from namespaced controllers. For example, consider a controller named which renders this template:

    The default setting is, which uses the partial at. Setting the value to would render, which is the same behavior as rendering from a non-namespaced controller such as.

  • determines whether should automatically disable on click, this defaults to.

  • determines whether to wrap the missing translations key in a tag or not. These defaults to.

  • determines whether generates remote forms or not.

  • determines whether generates ids on inputs.

  • determines whether forms are generated with a hidden tag that forces older versions of Internet Explorer to submit forms encoded in UTF-8. These defaults to.

  • determines whether to annotate rendered view with template file names. These defaults to.

  • determines whether and will generate a header that preload assets.

3.11 Configuring Action Mailbox

provides the following configuration options:

  • contains the logger used by Action Mailbox. It accepts a logger conforming to the interface of Log4r or the default Ruby Logger class. The default is.

  • accepts an indicating how long after processing records should be destroyed. It defaults to.

  • accepts a symbol indicating the Active Job queue to use for incineration jobs. When this option is, incineration jobs are sent to the default Active Job queue (see).

  • accepts a symbol indicating the Active Job queue to use for routing jobs. When this option is, routing jobs are sent to the default Active Job queue (see).

3.12 Configuring Action Mailer

There are a number of settings available on:

  • sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself. You should only use this if you have a different configuration for Action Controller, otherwise use.

  • accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action Mailer. Set to disable logging.

  • allows detailed configuration for the delivery method. It accepts a hash of options, which can include any of these options:

    • - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
    • - On the off chance that your mail server doesn't run on port 25, you can change it.
    • - If you need to specify a HELO domain, you can do it here.
    • - If your mail server requires authentication, set the username in this setting.
    • - If your mail server requires authentication, set the password in this setting.
    • - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of,,.
    • - Detects if STARTTLS is enabled in your SMTP server and starts to use it. It defaults to.
    • - When using TLS, you can set how OpenSSL checks the certificate. This is useful if you need to validate a self-signed and / or a wildcard certificate. This can be one of the OpenSSL verify constants, or - or the constant directly or, respectively.
    • - Enables the SMTP connection to use SMTP / TLS (SMTPS: SMTP over direct TLS connection).
  • allows detailed configuration for the delivery method. It accepts a hash of options, which can include any of these options:

    • - The location of the sendmail executable. Defaults to.
    • - The command line arguments. Defaults to.
  • Species whether to raise an error if email delivery cannot be completed. It defaults to.

  • defines the delivery method and defaults to. See the configuration section in the Action Mailer guide for more info.

  • specifies whether mail will actually be delivered and is true by default. It can be convenient to set it to for testing.

  • configures Action Mailer defaults. Use to set options like or for every mailer. These default to: