Static Pages in Rails
I just read an interesting post on how to handle static pages in rails and I had a different approach that I wanted to share. There's lots of different ways to do this and each method has its own merit.
When I say static page, I mean any page that doesn't fit into your other controllers. This might be an about us or a contact page or anything else that might not require a dedicated controller to handle.
In routes.rb, I would set something like this up:
map.static '/static/:id', :controller => "static", :action => "show"
We can grab the name of the page from params[:id] in the controller. Feel free to change the name 'id' some something else if it gives you that wrong feeling to treat something as an id that isn't really an id.
Next, I define a controller called static.
class StaticController < ApplicationController
def show
short_name = params[:id]
begin
render short_name
rescue ActionView::MissingPageTemplate
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end
end
end
So, what's happening here? We know we have an params[:id] or the route wouldn't match. We then call a begin block, so that we can rescue the exception. Which, in our case, is if someone passes an invalid filename through or the page hasn't yet been defined under app/views/static/pagename.html.erb. We just handle it as a 404.
What about if we want our static pages to live in a namespace without /static at the beginning ? ie. /about instead of /static/about
Define a regular expression that matches all the pages you want to support.
STATIC_PAGES = %w(about contact fun_facts)
STATIC_REGEXP = %r{#{STATIC_PAGES.join("|")}}
Then, in your routes file use the requirements parameter to match only these pages.
map.static ':id', :controller => "static", :action => "show", :requirements => {:id => STATIC_REGEXP}
This is just another approach and there are lots of ways to do this. I was inspired by Yehuda Katz to write this as he recently wrote a post about giving back to the community. Also, be sure to check out his posts about rails 3.
Posted by Luke Cowell on February 11, 2010 at 09:22 AM
Fix for inDesign CS3 crashes
If inDesign is crashing when you quit it, launch it, when you activate fonts or at any other time, you should consider removing or replacing the following file:
$HOME/Library/Preferences/Adobe\ InDesign/Version\ 5.0/InDesign\ Defaults
It appears to contain font information among other things. You care about this because if you were to have activated a corrupt font, or a font with a duplicate postscript name, your problems may continue even after you resolve the font issue because that font will still be cached in here.
eg.
... @Semibold @MyriadPro-Semibold @Myriad Pro Semibold @Semibold @Myriad Pro Semibold 8@Version 2.007;PS 002.000;Core 1.0.38;makeotf.lib1.7.9032 @Semibold Italic @MyriadPro-SemiboldIt @Myriad Pro Semibold Italic @Semibold Italic ...
Posted by Luke Cowell on October 01, 2009 at 12:17 PM
iPhone recovery mode vs. DFU mode
DFU mode and recovery mode are different. Here's how to get tot either one.
To put your phone in DFU mode:
- Turn your iPhone off
- press and hold the home and sleep/wake button for 10 seconds
- release the sleep wake button and hold the home button for 10 more seconds
To put your phone in recovery mode follow these steps:
- Disconnect the USB cable from the iPhone
- Turn your iPhone off
- Press and hold the home button while connecting the USB cable to your device.
- iTunes should now display a message to indicate you are in recovery mode
Posted by Luke Cowell on September 26, 2009 at 06:46 AM
Installing rmagick on OS X
For some reason the gem tries to build against a PPC library, which on my intel machine, doesn't not work.
sudo su -
ARCHFLAGS='-arch i386' gem install rmagick
Note: you might need to fix the quotes.
Posted by Luke Cowell on June 01, 2009 at 09:59 PM
Linotype FontExplorer Settings via command line
Finding setting keys for Linotype Font Explorer
There's lots more and they're easy to figure out.
defaults read com.linotype.FontExplorerX > a
Now make your setting change in Font Explorer - it works best to quit at this point.
defaults read com.linotype.FontExplorerX > a
diff a b
The output of the diff will tell you what keys changed.
This output from diff:
fontRequestsAutoActivate = 1;
Becomes:
defaults write com.linotype.FontExplorerX fontRequestsAutoActivate 1
A list of keys I've found useful
Here are a list of linotype keys that I've found useful in configuring workstations settings.
If a font from a different path with the same name is imported, remove the old one and re-add
defaults write com.linotype.FontExplorerX sameFont_importingAdjustments 1
defaults write com.linotype.FontExplorerX sameFont_useGlobalImportingAdjustments 1
Remove and re-add a font if imported from the same path
defaults write com.linotype.FontExplorerX samePath_importingAdjustments
defaults write com.linotype.FontExplorerX samePath_useGlobalImportingAdjustments
When a duplicate font is requested deactivate the current font and activate the requested font without warning
defaults write com.linotype.FontExplorerX hideActivationWarning 1
defaults write com.linotype.FontExplorerX hideActivationWarningOption 1
When activating a font activate the entire suitcase
defaults write com.linotype.FontExplorerX alwaysActivateSuitcase 1
Deactivate Fonts when linotype quits
defaults write com.linotype.FontExplorerX deactivateFontsFromSessionOnQuit 1
Autoactivate fonts when possible
defaults write com.linotype.FontExplorerX fontRequestsAutoActivate 1