Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 872
NolanDC Avatar asked Dec 09 '25 15:12

NolanDC


2 Answers

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.

like image 81
Eifion Avatar answered Dec 12 '25 10:12

Eifion


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.

like image 40
Simone Carletti Avatar answered Dec 12 '25 11:12

Simone Carletti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!