Building and Working With Static Sites in Ruby on Rails

Post on 12-May-2015

8.239 views 5 download

Tags:

description

Ben Scofield presents his solution for building web applications in Rails that contain a large chunk of static content. This approach merges the management of static content with the power that the Rails framework provides to developers.

Transcript of Building and Working With Static Sites in Ruby on Rails

© Copyright 2007 Viget Labs, LLC – www.viget.com

May 18, 2007

Building and Working with Static Sites in Ruby on RailsBen Scofieldben@viget.com

Senior Developer, Viget Labs

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Me

• 8 years building web apps (Perl, VBScript, C#, Java, PHP)

• Ruby and Rails for 2 years• Tech Lead for Seth Godin’s Squidoo• ben@viget.com

2

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Viget Labs

• Full-service web design and development consultancy

• Three types of clients– Flash-based entertainment sites

– Web 2.0 startups– Mid-sized, mostly static sites

3

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

“Static” sites

• Fully static: CMS• Small scale: About Us, Contact Info• Large scale: ACVA (funside.com)

4

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

5

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Hybrid sites

• Distinct static and dynamic content (About Us) • Intermingled static and dynamic content

6

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

THE PROBLEM

• Building a static site should be easy

7

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

And in PHP, it is

• Drop a file and see it run, even with embedded PHP code

• Use configuration files to route requests, set page-specific data, handle redirects, set page metadata...

8

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

But in Rails?

• Actions for every view? sigh • Actionless views? pfft

• and what about page-specific data?

9

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

So why bother with Rails?

• Less interesting: morale• More interesting: productivity

– task switching and specialization

• Most interesting: hybrid sites– current and future

10

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Notes

• Error handling - ignore its absence• Historical tour, Edge at the time

11

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Solution 1: Rails on PHP

• Routing + Convention + Action + Caching =basic PHP?

12

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Routing

• The beauty of the catch-all route:

• Declare all other routes explicitly!or namespace the catch-all:

13

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Convention

• All templates under a single app/views subfolder

• Move as much to the layout as possible

14

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Action

15

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Caching

• Short and sweet: caches_page :show• Rails caches under the request URI, not the

controlller/action

16

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Problems with Solution #1

• Manage page data in the views? pfft• Manually expire the cache? pfft• No support for redirects (site revisions)? pfft

17

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Solution 2: Configuratron 2000

• Use configuration files to overcome the problems of Solution 1

18

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

navigation.yml

• Central management for pages, page data, and relationships

19

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Memory’s cheap, right?

• Load it all into memory

• and pull it out when you need it

20

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Caching

• Expire the entire cache when navigation.yml is updated

21

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

redirects.yml

• Use the redirect entry to find the new page

22

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Problems with Solution #2

• Keeping it all in memory? pfft• Upload new config files to change content? pfft• All-or-nothing caching? pfft• Not very Rails Wayish, is it?

23

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Solution 3: CRUDification

• So what is Rails best at? – CRUD!

• Can we make static sites CRUDdier without creating a full-blown CMS?

24

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Move it all to the DB...

25

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Pages and Variables

26

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

27

Redirects

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Sitemaps for nothing

28

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Cascading Variables for Free

29

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Web UI

30

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Cache sweeping made easy

31

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Problems with Solution #3

• Search• Portability

32

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Solution 4: Feed the DB

• Store page content in the DB• Upload files or enter content through the UI• Search with ... whatever

33

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Reduce, Reuse, Recycle

• Package the framework for distribution

34

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Question 1: To Engine or To Plugin?

• Engines– Cons: deprecated, people yell at you

• Plugins– Pros: everybody loves plugins!

35

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Question 2: To Generate or To App?

• Generators– Pros: customized code

– Cons: hard to update

• Plugin Applications– Pros: easy to update, fast start

– Cons: possible conflicts

36

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Plugin Applications

• Miniature Rails applications - controllers, models, and views

• Grant complete, self-contained functionality to another application

37

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Using the framework

• Install the plugin-app• Update config files

– environment.rb and routes.rb

– (optional) .htaccess and environment.rb

• Run the migrations• Enjoy your newly more-static site!

38

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Key Points

• The catch-all route is the best kind of magic• Play to Rails’ strengths• Handle both large- and small-scale static sites

39

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

Get the (alpha) framework athttp://www.viget.com/railsconf/

Questions?Ask ‘em now or email me at

ben@viget.com

40

May 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com

Viget Labswe build web business

No questions, eh?

• Why even bother with this? Why not a CMS?• Use content_for in the views to move more to

the layout• Plugems vs. engines vs. plugins?• More about plugin-apps?

41