Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Epic static page rendering controller with support for nested pages, and routes at the root.
Ruby JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app/controllers/blue_sparks
config
lib
test
.gitignore
.rvmrc
Gemfile
Gemfile.lock
MIT-LICENSE
README.md
Rakefile
blue_sparks.gemspec

README.md

BlueSparks

Epic static page rendering controller with support for nested pages, and routes at the root. Heavily inspired by thoughtbot's high_voltage.

Downsides

Downsides, you say? Yeah, unfortunately the only downside is that we have to use a wildcard route to accomplish the "routes at the root" thing. This means that requests that would normally result in a 404 get routed through BlueSparks::PagesController first. This shouldn't cause any major hiccups, though. If you're still interested, read on for great justice!!!

Routes at the root?

Yeah, I'm talking about awesome routes like:

/mypage

Instead of:

/pages/mypage

Static pages?

Yeah, like "About us", "Directions", marketing pages, etc.

Installation

% gem install blue_sparks

Include in your Gemfile:

gem "blue_sparks"

Sorry, folks, Rails 3 only.

Usage

Write your static pages and put them in the RAILS_ROOT/app/views/pages directory.

% mkdir app/views/pages
% touch app/views/pages/about.html.erb

Here's the really cool part - you can nest pages.

% mkdir app/views/pages/somefolder
% touch app/views/pages/somefolder/index.html.erb
% touch app/views/pages/somefolder/other.html.erb

Now you'll be able to go to /somefolder and /somefolder/other. You can even nest these babies as deep as you like. Have a hundred folder nesting if you want. AWESOME, RIGHT?!

Routes

By default, the static page routes will be like /:slug (where :slug is the view filename).

If you want to route to a static page in another location (for example, a homepage), do this:

match '/my/crazy/page/of/doom' => 'blue_sparks/pages#show', :slug => 'mycrazypageofdoom'

In that case, you'd need an app/views/pages/mycrazypageofdoom.html.erb file.

Generally speaking, you need to route to the 'show' action with a :slug param of the view filename.

You can route the root url to a high voltage page like this:

root :to => 'blue_sparks/pages#show', :slug => 'home'

Which will render a homepage from app/views/pages/home.html.erb

Override

Most common reasons to override?

  • You need authentication around the pages to make sure a user is signed in.
  • You need to render different layouts for different pages.

Create a PagesController of your own:

% rails generate controller pages

Override the default route:

# in config/routes.rb
match '/*slug' => 'pages#show'

Then modify it to subclass from BlueSparks, adding whatever you need:

class PagesController < BlueSparks::PagesController
  before_filter :authenticate
  layout :layout_for_page

  protected
    def layout_for_page
      case params[:slug]
      when 'home'
        'home'
      else
        'application'
      end
    end
end

Testing

Just a suggestion, but you can test your pages using Shoulda pretty easily:

class PagesControllerTest < ActionController::TestCase
  tests PagesController

  %w(earn_money screencast about contact).each do |page|
    context "on GET to /#{page}" do
      setup { get :show, :slug => page }

      should_respond_with :success
      should_render_template page
    end
  end
end

If you're not using a custom PagesController be sure to test BlueSparks::PagesController instead.

Enjoy!

Credits

thoughtbot

for their awesome high_voltage gem.

The names and logos for thoughtbot are trademarks of thoughtbot, inc.

License

BlueSparks is Copyright © 2011 Cody Krieger. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

Something went wrong with that request. Please try again.