Building and Working With Static Sites in Ruby on Rails

41
© Copyright 2007 Viget Labs, LLC – www.viget.com May 18, 2007 Building and Working with Static Sites in Ruby on Rails Ben Scofield [email protected] Senior Developer, Viget Labs

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

Page 1: 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 [email protected]

Senior Developer, Viget Labs

Page 2: Building and Working With Static Sites in Ruby on Rails

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• [email protected]

2

Page 3: Building and Working With Static Sites in Ruby on Rails

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

Page 4: Building and Working With Static Sites in Ruby on Rails

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

Page 5: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

5

Page 6: Building and Working With Static Sites in Ruby on Rails

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

Page 7: Building and Working With Static Sites in Ruby on Rails

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

Page 8: Building and Working With Static Sites in Ruby on Rails

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

Page 9: Building and Working With Static Sites in Ruby on Rails

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

Page 10: Building and Working With Static Sites in Ruby on Rails

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

Page 11: Building and Working With Static Sites in Ruby on Rails

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

Page 12: Building and Working With Static Sites in Ruby on Rails

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

Page 13: Building and Working With Static Sites in Ruby on Rails

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

Page 14: Building and Working With Static Sites in Ruby on Rails

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

Page 15: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Action

15

Page 16: Building and Working With Static Sites in Ruby on Rails

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

Page 17: Building and Working With Static Sites in Ruby on Rails

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

Page 18: Building and Working With Static Sites in Ruby on Rails

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

Page 19: Building and Working With Static Sites in Ruby on Rails

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

Page 20: Building and Working With Static Sites in Ruby on Rails

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

Page 21: Building and Working With Static Sites in Ruby on Rails

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

Page 22: Building and Working With Static Sites in Ruby on Rails

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

Page 23: Building and Working With Static Sites in Ruby on Rails

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

Page 24: Building and Working With Static Sites in Ruby on Rails

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

Page 25: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Move it all to the DB...

25

Page 26: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Pages and Variables

26

Page 27: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

27

Redirects

Page 28: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Sitemaps for nothing

28

Page 29: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Cascading Variables for Free

29

Page 30: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Web UI

30

Page 31: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Cache sweeping made easy

31

Page 32: Building and Working With Static Sites in Ruby on Rails

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

Viget Labswe build web business

Problems with Solution #3

• Search• Portability

32

Page 33: Building and Working With Static Sites in Ruby on Rails

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

Page 34: Building and Working With Static Sites in Ruby on Rails

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

Page 35: Building and Working With Static Sites in Ruby on Rails

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

Page 36: Building and Working With Static Sites in Ruby on Rails

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

Page 37: Building and Working With Static Sites in Ruby on Rails

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

Page 38: Building and Working With Static Sites in Ruby on Rails

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

Page 39: Building and Working With Static Sites in Ruby on Rails

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

Page 40: Building and Working With Static Sites in Ruby on Rails

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

[email protected]

40

Page 41: Building and Working With Static Sites in Ruby on Rails

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