How we built a blog for our Rails App in less than 30 minutes

How we built a blog for our Rails App in less than 30 minutes

A blog can be a essential part of a web application and for Rails there are many ready to use solutions out there, but why not bake your own? Especially it may is easier than you thought!

Most creators in the web had to make the decision to build their own solution or to use something existing several times. Often it is smarter to use something that already exists, but lets face it. A blog is essentially just a list of articles. Plus a article view and maybe a RSS feed. This is CRUD so the perfect job for Rails.

We assume you have a configuration like: Ruby on Rails, rails_admin, Paperclip, metamagic & Bootstrap. Otherwise you need to tweak some details.

Creating the scaffold

rails g scaffold Blog title:string keywords:string category:string description:string body:text

I am sure many of you are now mad already. "How is category no relation?" "Why is keywords a string?" but lets face it, it is all we actually need for a rather small blog. Just read on and see where this is going.

I now removed a lot of the generated stuff, actually most of it. No need for .json builders, new or edit. We use rails_admin in the backend so this is where we are going to create and edit blog posts. If you want to keep the scaffold make sure to protect it with devise or anything. But using a Admin Interface is helpful anyway so i would rather recommend looking into these.

Edit blogs_controller.rb

What we have done here is remove all the stuff we don't need anymore and modify the relevant methods. We also added a feed method to offer a RSS feed. The second line in the index method is all we need to filter by categories which is a feature we want as well. You maybe also want to filter by tags, but we only use them for the metatag in our blog.

Edit routes.rb

What we have done here is editing our routes so the path is /blog/ instead of /blogs/. We also added the RSS feed route plus a redirector as we noticed some client automatically looking for /rss. We also limited the blogs resource to only :index and :show

Add blog post avatars

We use paperclip for attachments, it comes with a generator so all we had to do is: rails generate paperclip blog avatar and add the following to the models/blog.rb file:

You can define your own sizes, for our needs these 3 defined sizes are what we need. We let everything else default.

While we are on it we also make use of to_param, this is why our blog post contains the title and not only the ID. By keeping the ID in front of the title this is all we have to do to make this working. To make it work we replace all spaces from the title with "-" and then remove any special chars. Close to perfect SEO friendly URLs.

Create _sidebar.html.erb in views/blogs

Nobody likes to repeat themselves so we created a _sidebar.html.erb we can include in our index and show templates. Notice the method that collects the categories? This is why we don't need a relation here. We assume that the few people who actually write blog posts will reuse the category, otherwise its easy to correct as well.

Edit index.html.erb

Now lets edit index.html.erb here is where metamagic comes into the play to create metatags easily.

Nothing special happening here, we iterate over the blog posts and print them out with image, title and description. This is all we need. Please note that we have defined twitter and og cards in the application.html.erb so we only need to overwrite a few details here instead of defining complete tags.

Edit show.html.erb

A little more magic is happening here. First we define all our metatags including the keywords we simply split into a array here. We also show 3 other random blog posts on the bottom to have some internal link juice, which is always a good idea. Obviously this is not optimal, but it works great.

Create feed.rss.builder

Our last step is to create the RSS template. You need to edit some details in here for your own needs but this is how ours looks like. It will automatically create a RSS feed out of all your articles. We decided for no limits, but will ad some when we hit about 15-20 posts.

Thats it!

Obviously this is not a optimal way to create a blog, but it works and looks nice! Which is all we really need right now. When there is more traffic we surely have to think about removing the queries in the templates and include some caching. But for the maybe 100 views we get on a day this is good enough.

No need for a blog engine, Wordpress or anything else. When you can spin up a blog within minutes. Obviously pagination will be a topic anytime soon, but for now its not necessary and when it gets relevant Kaminari is just a few lines of code away.

Other articles