We’ll be hosting blogs for Agency members on our site. Members have CRUD actions on their blog posts. Admins will be able to “feature” specific blog posts such that they show up in a publicly accessible page.
We need a way to support more robust formatting for the text that users write into their blogs. ActionText with the Trix editor is built into Rails. From some light research, it seems like it shouldn’t be too difficult to setup
Pros
We need a model to persist blog posts.
# migration
def change
create_table :blog_posts do |t|
t.string :title, null: false
t.text :content, null: false
t.references :user, null: false, foreign_key: true # or `author` ?
t.integer :status, null: false, default: 0
t.string :slug
end
add_index :blog_posts, :slug, unique: true
end
# app/models/blog.rb
class BlogPost < ApplicationRecord
belongs_to :user
enum status: {
draft: 0,
published: 1
}
end
We’ll want a slug for giving human-reader friendly URLs. This will probably be something we can have generated automatically with the friendly_id
gem.
A Blog
model could function as a collection of BlogPosts
.
# blog post migration
def change
create_table :blogs do |t|
t.references :user, null: false, foreign_key: true
end
add_reference :blog_posts, :blog
end
class Blog < ApplicationRecord
belongs_to :user
end
class BlogPost < ApplicationRecord
belongs_to :blog
end
This could allow for users to have multiple blogs.
Pros