Semi Static Pages in Rails 5

In this article, I will highlight the Rails 5 related changes required to get the railscasts episode on Semi Static Pages working. Create a new Rails 5 project.

rails new static

Copy the seeds.rb from the source code repo to your project. Create the product and page models.

rails g model product name price:decimal released_on:date
rails g model page name permalink content:text 

Change the generated migration file to define an index for permalink column.

add_index :pages, :permalink

Generate a pages controller with all the 7 RESTful actions.

rails g controller pages index show new edit create update destroy

Copy the views for pages controller, _form.html.erb, edit.html.erb, index.html.erb, new.html.erb and show.html.erb. Generate a info controller with about, privacy and license actions.

rails g controller info about privacy license

The info/about.html.erb has static content. Migrate and populate the database.

rails db:migrate
rails db:seed

Generate the products controller with index and show actions.

rails g controller products index show

We also need to change the ActiveRecord syntax in pages controller.

Page.find_by!(permalink: params[:id])

You can browse the solution 1 by checking out the hash 9268502de02d62c7acf3962c3a649194be289ed9 and solution 2 by checking out the hash d020a3ca61dc12e00502fd6c622c14b14c7191fb.

Summary

In this article, you learned how to implement semi static pages in Rails 5.


Related Articles


Create your own user feedback survey