Filepress is a minimal Rails plugin that offers the best of both worlds for content management:
- Write and version control your content in Markdown (or a similar format)
- Sync it seamlessly to ActiveRecord models so you can harness the full power of a Rails backend.
Ideal for blogs, documentation, or any content-driven Rails app where you want the flexibility of flat files and the structure of a database.
- Install the gem
Add it to your Gemfile:
gem "filepress"
Then run:
bundle install
- Create a model to represent your content
Be sure to include a field that can serve as a unique identifier (e.g. slug
):
rails g model Post title:string slug:string body:text
- Enable Filepress for your model
Use the filepress
method in your model class:
class Post < ApplicationRecord
filepress
end
- Add some content
Create Markdown files in app/content/posts
. Each file should include frontmatter:
---
title: My First Post
slug: first
---
# Hello, this is my first post
- Sync your content
Run the sync task to import your files into the database:
bin/rails filepress:sync
That’s it! You’re now free to query your content via ActiveRecord like any other model. Filepress doesn't dictate how you render the body—use a Markdown parser like Kramdown, Redcarpet, or similar.
Filepress reads your content files, extracts YAML frontmatter, and uses the values to populate or update model attributes. The rest of the file becomes the value of the body attribute (or another field, if configured).
You can customize how Filepress behaves by passing options to filepress
:
class Post
filepress from: "app/my_custom_content_folder"
end
class Post
filepress glob: "*.html"
end
class Post
filepress key: :name
end
class Post
filepress body: :content
end
By default, Filepress deletes records when the corresponding file is removed. Disable this behavior with:
class Post
filepress destroy_stale: false
end
Filepress is for you if:
- You prefer writing content in files
- You want the convenience of version control for content
- And you still want powerful querying, associations, validations and all the other Rails goodness