Static data
Accessing and storing static YAML, JSON and XML files.
Disambiguation
This documentation page is not about serving static assets or rendering static files.
For serving static assets (images, scripts, stylesheets, fonts, etc.) view the asset pipeline documentation page.
For rendering other static files, view the rendering static files documentation page
Description
The static
directory should contain structured data in the YAML, JSON or XML formats. Other files can be stored here for the purpose of rendering them, but only files of these formats can be accessed.
Currently supported extensions are:
.xml
,.yaml
,.yml
,.json
and.geojson
.
The Static
helper constant can be accessed from anywhere within your application. This constant acts as a link to the static
directory, allowing you to access data.
Examples
The data in a
.geojson
file atstatic/server-locations.geojson
can be accessed with:NOTE: The file name is automatically inflected to create a valid Ruby method name. This may lead to differences between the file name and method name as seen in this example (with the
-
being changed to a_
).The data in a
.json
file atstatic/dependencies/config.json
can be accessed with:NOTE: Directory names are also inflected in the same way as file names.
The data in a
.yml
file atstatic/locales.en.yml
can be accessed with:
Viewing defined sub-directory and file methods
The Object#methods
method can be used to check for defined sub-directory and file methods for the Static
helper object.
Consider the following directory structure:
Symbolizing
Data that is read from the files in the static
directory is automatically converted into a hash.
Since it is Ruby convention that hash keys are symbols, all hash keys accessed through this helper will be symbolized by default (this can be changed in the core application file, however).
Examples
Consider the following .yml
file:
If configured to symbolize the generated hash, then the data for the Rails
framework can be accessed with:
If configured to not symbolize the generated hash, then the data for the Eucalypt
framework can be accessed with:
Last updated