In this lesson, we create our first database table and Rails model.
Putting the Page Contents into the Database
In the previous lesson, we created static pages, with all the contents in the view files. The problem with this approach is that to change any text, you must modify the code.
A better approach is to put the page contents into the database, so we can then provide an administrative interface that allows non-technical users to modify the pages. In this lesson, we do just that, creating the core of a simple content management system (CMS).
We begin with the code with which we ended Lesson 9. These zip files contain the beginning and ending states of the code:
- Learning Rails example app code as of the end of Lesson 9
- Learning Rails example app code as of the end of Lesson 10
See Lesson 8 for pointers on setting up your development environment.
In Lesson 8, we explained how to set up the MySQL database. In this lesson, we use SQLite, which is a little easier to get going (if you’re using Leopard, you don’t have to do anything to use SQLite). If you want to use MySQL instead, you just need to change the settings in database.yml. In particular, in the development section, replace the code in the sample application with something like this:
adapter: mysql database: learning_rails username: root (or other user name) password: secretword (whatever your password is; leave blank if none) host: localhost
You’ll also need to create the database, using the mysql command-line interface or one of the GUI interfaces (we’re partial to Navicat). With sqlite, this is done for you automatically.
Here’s the coding steps we take in this lesson. Watch the screencast for details and explanations (see link at the top of the left column).
1. Delete the four static views in views/pages
2. Create a scaffold for the new Page model with this command:
script/generate scaffold page name:string title:string body:text
3. Run the migration:
4. Start the server:
5. Delete the automatically generated layout file
6. Create the four pages (home, about, contact, resources) through the admin interface:
Browse to http://localhost:3000/pages/new
7. Create a controller and view for the public view of the page:
script/generate controller viewer show
8. In controllers/viewer_controller.rb, add this line to the show method:
@page = Page.find_by_name(params[:name])
9. Replace the boilerplate text in views/viewer/show.html.erb with this line:
<%= @page.body %>
10. You can now access the home page using the explicit URL:
11. Create a route to clean this up. Add this line to config/routes.rb, after the map.resources line:
map.view_page ':name', :controller => 'viewer', :action => 'show'
12. Update the route for the root (home) page (we created this line in Lesson 9):
map.root :controller => 'viewer', :action => 'show', :name => 'home'
13. Fix the navigation links in views/layouts/application.html.erb:
<li><%= link_to 'Home', view_page_path('home') %></li> <li><%= link_to 'Resources', view_page_path('resources') %></li> <li><%= link_to 'About Us', view_page_path('about') %></li> <li><%= link_to 'Contact Us', view_page_path('contact') %></li>
14. Change the title tag line so that the title is pulled from the database:
<title><%= @page.title %></title>
This will work for our normal pages, but it won’t work for the admin pages, since there isn’t always a valid @page object for those pages. So we need another way to set the page title for those pages. We change the layout to this:
<title><%= @pagetitle || @page.title %></title>
|| is the Ruby OR operator. If the item before the OR is not false, then the second item is not evaluated, so this expression essential says “use @pagetitle, unless it is nil; if it is nil, then use @page.title”. This enables us to set the pagetitle with an explicit instance variable in the admin controllers, and still use the @page object in the normal page controller.
Now we need to set @pagetitle in the pages_controller. We want to set it to “Page Administration,” regardless of which action is executed. Rather than placing this code in every action, we can use a before filter, which runs the filter action before any other controller action. The code, which goes at the top of pages_controller.rb, is as follows:
before_filter :loadmetadata def loadmetadata @pagetitle = "Page Administration" end
Note that the home page route that we set requires that you create a page named home in the database. If you haven’t created a page with this name, you’ll get an error when browsing to the base URL. (To create a page in the database, browse to localhost:3000/pages/new)
Also, the navigation buttons assume there are pages with the names of about, resources, and contact, so if you haven’t created pages with those exact names in the database, you’ll get an error when you click on the corresponding button.
We’re back to where we started! The site now works just like the one we created in Lesson 9, but now all the page contents are in the database, and we have a simple administrative interface to edit this text.
There’s one big problem with this simple implementation: anyone who can guess the URLs for accessing our admin interface can modify the site! In the next lesson, we’ll add a user log-in system to provide authentication for accessing the admin interface.
For more on the SQLite database, see our list of SQLite 3 tutorials, software, and FAQs.
Another Rails 2.0 scaffolding tutorial.
Ready to dive deeper into Rails? See our list of Ruby on Rails books