简体   繁体   中英

Creating non-standard pages with Ruby on Rails

I'm creating a personal site with Ruby on Rails. For the most part, RoR is the perfect tool, allowing for blog posts, comments, etc all rather easy.

However, I want to have a few pages that don't require any specific model. An 'About Me' page, for instance, is necessary, and yet doesn't warrant it's own model/controller. In addition, these 'singleton' pages will be linked to from my default layout, and must be accessible even when there are no objects created.

Is there a good way to handle this? I've seen many RoR sites that have single pages while maintaining pretty urls, but never an example of how it's structured. Finally, is it possible to make these single pages dynamic? I'd rather not have static html if at all avoidable.

There's a Railscast about this subject which might answer your question:

http://railscasts.com/episodes/117-semi-static-pages

I've used this solution a few times in my Rails applications.

I usually create a "static" controller, for instance an AboutController .

ruby script/generate controller about

Then I create as many action as my about pages: index , contact , terms ... Then I add a generic route in my routes.rb file.

map.about 'about/:action', :controller => "about"

In my pages, I reference a single page as

<%= link_to "Contact", about_path(:action => "contact") %>

Because they are static pages, you can also consider to cache them in your controller.

class AboutController < ApplicationController
  caches_page :index, :contact, ...
end

This architecture fits well for the most part of static pages. If you want "semi-static" pages you might consider to dynamically load the content from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM