Dynamic Named Routes for Semi-Static Pages in Rails
When I was designing the new UMSwing website, I had a few issues that, at the time, I didn't have a clean method of implementing. One of those was the creation of semi-static pages. After watching this episode of Railscasts, I had a pretty good idea of how to implement them. The only issue with the solution offered was the lack of dynamically generated routes.
Semi-static pages are used everywhere on websites. They're those pages like an "About" page, which has content on it that doesn't really change that often. Typically, a controller would have to house these actions (/about, /faq, /contact, etc.), and the routes specified manually. Railscasts came up with an ingenious idea to create a controller which was routed to /static/*, so that semi-static pages could be created on-the-fly and modified easily. It also allows for modifications to change without committing to a repository and going through the process of deploying all over again.
For those needing a quick Rails primer before going on, here's the quick and dirty of what you need to know to understand this:
- Rails is a MVC-based web application framework that runs on Ruby. In short, Ruby code is written to create webpages on-the-fly.
- Every request in Rails is first put through the routes file in config/routes.rb. This file tells Rails which Controller and Action is run.
Okay, let's get started. Let's create our static pages scaffold (which includes model, views, and the controller). Obviously, there are sections of this that you would want to require authentication for (editing and deleting, for example), but that's outside the scope of this tutorial.
script/generate scaffold pages title:string permalink:string content:text;
rake db:migrateNow we need to modify our controller slightly. More specifically, our show action. Right now, it will respond to showing an element only when the ID is displayed. We want to modify it to handle a permalink as well (/about and /contact look better than /pages/135, don't you think?). Here is your modified show action:
1 2 3 4 5 6 7 | def show if params[:permalink] @page = Page.find_by_permalink(params[:permalink]) else @page = Page.find(params[:id]) end end |
Before we go any further, we need to create two custom methods in our model. These will format the permalink to remove any unwanted characters for the custom route name (replacing all unacceptable characters with an underscore) and for the URL (replacing all unacceptable characters with a forward slash to allow for nesting of pages). It's also important to note here that previous validation should be done to ensure that the permalink does not have leading or tailing non-alphanumeric characters, but I removed that for simplicity's sake.
1 2 3 4 5 6 7 8 9 10 | class Page < ActiveRecord::Base def route_name p = self.permalink.gsub(/([^A-Za-z0-9])+/, '_').downcase # Change non-alphanumeric characters to an underscore "static_#{p}" end def uri self.permalink.gsub(/([^A-Za-z0-9])+/, '/').downcase # Change non-alphanumeric characters to a forward slash end end |
At this point, we can create and modify our pages as we would regularly expect from a new controller. All of our pages are accessible via /pages/1, /pages/2 etc. We now need to make our controller act as our catch-all (so that all requests that do not match any of the other controllers get routed to our Pages controller), and we also need to provide permalink support. Finally, we will dynamically generate customized, name routes for all of our semi-static pages. All of that gets accomplished with a few short lines of code. Add the following code to the top of your config/routes.rb file, starting at line 2 (inside the ActionController::Routing::Routes.draw section):
2 3 4 5 6 7 | def map.static_page_actions pages = Page.find(:all) pages.each do |page| self.send("static_#{page.route_name}", "#{page.uri}", :controller => "Pages", :action => "show", :permalink => page.permalink) end end |
Finally, we need to call this method close to the bottom of the code, right before our default catch-all routes.
map.static_page_actions map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'
What this method does is retrieve all of the static pages in the database, then creates a customized, named route for each page, telling Rails what each URI should look like, and where to direct the request to.
Hopefully this helps some people out with their dynamic page creation. I'm pretty sure there's a pitfall or two here, but I think it could be taken care of by doing some simple route housecleaning in the Pages CRUD controller. The perk of this option is that it allows the routes to be named, and hopefully that is of some benefit for others.
Fix Your Facebook Privacy Settings
Earlier this week, Facebook rolled out a new privacy policy which allows outside applications to view information stored on you, including your likes, connections, education, current city, and more. Needless to say, there's a big issue with privacy here. While you want your friends to be able to see this information, you want to avoid giving it to 3rd parties as much as possible. Here's some key tips to locking down your profile from those automated prying eyes.
1. Remove Instant Personalization [link]
Instant personalization gives your information to 3rd party websites such as Docs.com, Pandora, and Yelp. Uncheck this box to prevent these sites from accessing your information.
2. Remove Unused (and sketchy) Applications [link]
Chances are you've added some application at some point, and although you deleted it off your profile, it probably still has access to your profile. Remove any unwanted applications by going to the link above and deleting those apps which shouldn't be there. You might be surprised how many pages can see your information!
3. Remove Your Public Profile [link]
Data mining will largely rely on your public profile as a starting point for gathering your information. Remove that ability by going to the link above. Change your Facebook Search Results to Friends and Networks, and then Uncheck the option to have a public profile.
4. Lock Down Your Contact Information [link]
On this page, you can find all of the contact methods that are available to you. Unless you really want anybody to contact you, it is best to set almost all of these to "Friends" and nothing else. The only exceptions are the option to add you as a friend, and to send you messages. Both are worthwhile to leave open to everybody unless you happen to get spam from them.
5. Lock Down Your Profile Information [link]
Finally, there's your actual profile information that should be locked down. Setting all of these to "Only Friends" is the best course of action.
---
If you haven't done so, lock down your information soon. I can guarantee that the automated data mining services are working full-tilt in case Facebook reverts its privacy settings. It's time to take control of your profile settings.
Backup and View Your iPhone SMS Messages For Free
Update: this does not work for iOS 4 and up, due to iTunes encrypting the backed up files. There is an easy fix for all of those with jailbroken devices, however. Stay tuned for that update!
Lately I wanted to backup some of my text messages from my iPhone 3GS onto my desktop, but couldn't figure out how to do that. After some quick research and some poking around, I was able to figure out how to view them quickly and easily.
Disclaimer
I am not responsible if you screw something up on your computer. It's not my problem if something breaks. Do this at your own risk (which should be pretty low, unless you're one of those people that shouldn't be allowed near a computer).
What you'll need
- SQLite Database Browser (available at http://sourceforge.net/projects/sqlitebrowser/files/)
Let's Get Started
In a nutshell, the SMS system on the iPhone is just a carefully hidden SQLite database. All we have to do is find the file and open it up in the SQLite Browser.
- First, we need to locate the file that contains the SMS messages, which will be either:
- 3d0d7e5fb2ce288813306e4d4636395e047a3d28.mdbackup
- 3d0d7e5fb2ce288813306e4d4636395e047a3d28.mddata
This will be in one of the following locations:
- Windows Vista/7: C:\Users\[Your User Name]\AppData\Roaming\Apple Computer\MobileSync\Backup\[iPhone ID]\
- Windows XP or lower: C:\Documents and Settings\[Your User Name\Application Data\Roaming\Apple Computer\MobileSync\Backup\[iPhone ID]\
- Mac OS X: User > Library > Application Support > MobileSync > Backup >[iPhone ID]
- Copy this file to a new location to protect the file in case you accidentally screw something up (your Desktop, for example).
- Open up the new copy of the file in SQlite Browser, then select the "Browse Data" tab. Finally, select the "message" table from the "Table:" dropdown box
And that's all it takes! From here, you can export this as a CSV via File -> Export -> Table as CSV so you can import it into Excel, or manipulate it however else you wish. If I get the time, I'm going to write a quick tool to nicely export the messages to PDF so that they look good instead of being in a table. But, it's a nice fix for wanting to go through them on a computer, or do fulltext searches with the content.
New Music Monday Recommendation: Professor Kliq and SXSW
I know I said I was going to write a post about backing up your iPhone SMS messages, but that'll have to wait until later today (it'll be out in the afternoon: I promise!).
Every once in a while, I stumble upon a great artist. I mean, really great. Yes, there are talented artists out there, many of which are mainstream in today's society. That isn't what makes this artist great, however. Yes, he's talented. Very talented, as I hope you'll find out when you listen to some of his music. What makes this artist great is that he distributes all of his music for free.
Yes. Free.
If you are even remotely interested in electronic music, you'll love Professor Kliq (Mike Else). Making music since 1996, he has been making music for almost fifteen years, experimenting with many different forms of electronic music, taking both grassroots and modern styles into his experimentation. He has put out six albums, one EP, and one set of remixes. Do yourself a favor and give this a listen; if you appreciated electronic music half as much as I do, you'll love this.
Secondly, if you love music, chances are you've heard of SXSW, or South By Southwest. SXSW Conferences & Festivals offer the unique convergence of original music, independent films, and emerging technologies. Even if you don't attend the events, many artists give out a free MP3 of their music to be distributed by SXSW on their website. The latest batch of 2010 entries was recently put up on BitTorrent, boasting a grand total of 1083 songs, and 5.43GB of disk space across two torrents. If you want the opportunity to be exposed to some amazing new music by independent artists who are freely (and legally!) giving away their music for you to listen to, check out the link below for more information.
March 5 2010 Photography Update
So I haven't put up any photos in a while. I've been on a bit of a dry spell, but I went through my archives and pulled out some that I think were better. Comments, as always, are welcome and encouraged.
Next Monday, I have an interesting post for all of you with iPhones. One thing I really like about the iPhone is that I don't have to delete text messages. I have a quick and free way for you to back up all of your text messages to read later on. There's a lot of paid services out there to do it, but you don't have to pay anyone for this quick-and-easy trick!
5 Goals For The Next 6 Months
Well, in an attempt to get back on my 'writing horse', I figured I'd start with some of the things that are rattling around my brain right now in terms of what I want to get accomplished in the short term. Some are computer-related, others are not. Nevertheless, I have some goals for things that I want to get done or accomplish in the next 6 months. Here are five of them.
1. Buy a new car
With the potential of a great new job headed my way (possibly more on that later), I should have some disposable income in the near future. To celebrate this, I think it's time to get a new car; my current car, although it holds 5 people, is hardly what you consider sporty, is developing a bad case of rust, guzzles gas like nobody's business, and isn't fun to drive at all. I plan on buying a 2003 Hyundai Tiburon with a 5-speed manual transmission. They come in around $9000 before taxes, so on a financing plan I'll be able to buy that no problem.
2. Build a customized car trunk sound system enclosure
I'd love to put my DIY skills to the test and create a proper molded sound system enclosure for my new car. It's a lot of work, but it'll be a lot of fun. This will also be very useful to work on some woodworking skills and some custom fabrication work.
3. Start my development website
For about two years, I ran a security-based site, but eventually shut it down because my interests simply did not coincide with my interests and life aspirations. As such, I've decided to start a development-based website. I can do the security-aspect within the programming and development, but I can also get into graphics design. It fits into my interests much more at this point in time, and I think it will be more beneficial to the internet as well.
4. Develop Thimbleberry
I have a new site in the works with a friend. That's about all I'll tell you for now. Either way, should be a fun time.
5. Start actively contributing to the open source community
The open source community is amazing. Those of you that have heard of a little operating system called Linux may know that it's open source. That's right: it's free to use, free to modify, and free to redistribute. There are tons of open source projects available online, and I'd like to start contributing to one or start a new one that people will actually use. I've been using open source software for a long time, and it's about time that I gave back to the community that has helped me out in so many ways.
Well, it may be feeble, but it's a start at getting back in the writing groove. Soon I'll start doing a bit more technology writing again, and will hopefully do some stuff on interface design. Thanks for sticking with me through my creative drought, and we will hopefully see you Monday.
Level 3 Studios Photo Shoot
After two weeks of dying to actually pick through these photos, I finally found the time this past weekend. My schedule has been ridiculously busy, and as such my blog updates have suffered; that precious buffer I had going: gone!
Anyway, this batch of photos is from my first photo shoot, which I did at Level 3 Studios with a bunch of friends. This was a great first experience in a photo shoot scenario, but also very daunting and intimidating. I'd love to do it again, but I might need some more ideas before I go into that.
Because I was rushed, I never had the chance to do any major photo manipulation with these, and I think a fair number of these will look better after some simple changes, such as a grey scale or sepia tone added to them. I'd love to do that to a selection of them and upload them later.
As per usual, I really appreciate your comments. Post a comment below; I read all of them, I promise!
February 1 Photography Update
Well, more photos! I have had people breathing down my neck for me to put these up, and I've finally given in. Because this week's update spans almost two weeks, I've got almost 50 photos here (48, to be exact), ranging from the hoar frost we had a while back, to the Black & White Swing Thing, to Oldies Night at the Legion.
As per usual, I really appreciate your comments. Post a comment below; I read all of them, I promise!
January 18 Photography Update
The latest batch of photos is in. This time, all are from this past week's Legion, where we got a great turnout. I was able to try out using my flash in slave mode, where it gets controller by a 580EX II which is mounted on my camera. Overall, I think I got some great shots this week, including one or two I'm considering getting prints made of (such as #11, the one of Sylvia & Kiral).
I also did a bit of shooting last night in my room with my macro lens of a plant I have. I love some of the colors that came out of the shots, and I included two at the end of the gallery.
Hope you enjoy, and feel free as always to comment below.
January 16 2010 Photography Update
Over the past week, UMSwing has had a recruitment table up in an attempt to get new members interested in the club. During that time, we do a lot of dancing (a couple hours a day), hoping that some people will be impressed by it. We think it's working, but we'll have to wait until the open house to see.
As always, I welcome your comments. Add a comment below!





















