Archive for the 'ruby on rails' Category
Shouldn’t Have Done This in Ruby …
Wednesday, July 18th, 2007@foo = @bar = [ ]
That causes foo and bar to refer to the same Object, and essentially be the same. After I did it in my code, I wondered why @foo and @bar yielded the same values when I iterated through them in my view.
It’s because I tricked myself.
@foo = [ ] @bar = [ ]
That works like I was expecting. Should have remembered that…
Ruby strftime without leading zeros
Wednesday, January 10th, 2007The Power of method_missing
Sunday, December 3rd, 2006Ruby’s method_missing (which essentially provides a rescue hook for any calls to methods that are missing - not defined) comes in pretty handy with Rails. (Now, I’m no Ruby on Rails guru, so take the following with a grain of salt, but I found it pretty effective.
Every URL maps to a controller then a view (after your URL, it’s /controller/view). What if a user tries to go to a view that’s not there? Say you have a controller called “foo” that has a view mapped to “bar.” /foo/bar works just fine. But if a user types in /foo/baz, you’re in a little bit of trouble. Here’s where method missing comes in: in your application.rb file, just add a declaration for method_missing and have method_missing take care of things for you by redirecting or otherwise re-routing that bad request. Here’s an example:
def method_missing(url) redirect_to :controller => "foo", :action => "bar" end
Any errant method calls (read: any attempts to visit a controller action that doesn’t exist) is now safely handled: you’ve essentially intercepted that phony URL in application.rb and handled it as gracefully as possible. You can redirect, render a special template, or anything else you’d like. A really, really neat trick!
technorati tags:ruby, on, rails, application.rb, method_missing
Overriding Ruby On Rails’s default database naming
Tuesday, November 28th, 2006Here’s one that took way too long to figure out (mostly because I couldn’t think of a good Google search for what I was looking for). Hope this entry has enough keyword stickiness to help someone else out.
To override ActiveRecord’s default naming schemes (pluralized table names, "id" as the name of the primary key), you can use the following ActiveRecord methods:
set_table_name "MY_TABLE_NAME"
set_primary_key "MY_KEY_NAME"
Those methods should override Ruby on Rails’s default behaviors and let you use your own names for ID/primary key fields and table names.
technorati tags:ruby, on, rails, activerecord, database
File upload content types on Ruby on Rails one-liner
Sunday, November 12th, 2006Need to save the content_type of a file upload in Ruby on Rails? Here’s a simple one-liner for your model that does it:
self.content_type = incoming_file.content_type.to_s.strip!
where incoming_file is a reference to the file that’s been uploaded. Why the strip! call? Because Rails (at least on Windows) stores an extra character (newline / carriage return) at the end of the content_type field. strip! (the bang method) drops that last character in place, letting you do everything in that single line.
technorati tags:rubyonrails, file, upload, ror
A Big Plug for RadRails
Friday, October 27th, 2006Yesterday at work, we held a Hack Day (for those unfamiliar, you get 24 hours to build a prototype on just about anything you want). My team chose to build a company intranet in Ruby on Rails. Why Rails? Because it’s fast and easy and is my language + framework of choice for web development. Gregg, one of our top Java guys and my teammate, suggested we use RadRails as our IDE. I’d downloaded it at home but never had a chance to test it out. Plus, I’m not usually a big Eclipse fan (RadRails is based on Eclipse). When the day was done, I was very impressed with what RadRails did.
RadRails really works as the center of your development environment, providing things like a server and quick access to generators to get development moving. It makes already fast things (like script/generate tasks) even faster (no need to leave the environment and jump to a terminal to run that script). Bottom line: RadRails streamlines your Ruby on Rails development workflow. Go get it!
technorati tags:radrails, ruby, rails, development
Blogged with Flock
Ruby on Rails: Selecting the last item in a database table
Thursday, October 26th, 2006Ruby on Rails has the great find(:first) method that lets you grab the very first ActiveRecord object out of a given database table. That’s all well and good, but what if you want the last? Use this: Model.find(:first, :order => “id DESC”). That will give you the first record in a reversed list of IDs: basically, the record with the highest ID value, or the last record in the table.
Not good enough for you? Order by any other field. Want a different first record ? Sort by a field ASC. The :first record really depends on what order your records are in.
technorati tags:rubyonrails, RoR, Ruby, Rails, ActiveRecord
RoR - Complete forum in under 500 lines of code
Friday, September 1st, 2006Beast: An open source Rails forum in under 500 lines of code. Yeah, that headline caught my eye, too. The thing is, I checked out the demo, and it looks great and is robust. They’ve definitely captured the everything you expect from a forum. Even if it was 10,000 lines of code, it’s great software; the "brevity" of Beast just makes it that much better. It’s open source, and is definitely worth a look if you’re rolling on Rails.
technorati tags:rails, ror, beast, forum
Blogged with Flock
Nice little Ruby on Rails random password generator
Sunday, August 20th, 2006Spits out a nice little alphanumeric password. Great for a random password generated on signup. Only a two-line function, to boot.
I'm Reading…
- Paul Irish and I enjoy a good web geek debate.
- Tom Kershaw is a diamond geezer.
Search This Site
You are currently browsing the archives for the ruby on rails category.Archives
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
Categories
- adobe air (2)
- browser bugs (3)
- code (44)
- cross browser (10)
- css (34)
- design (23)
- development (5)
- dhtml (29)
- interaction design (2)
- javascript (54)
- learn (33)
- personal (3)
- projects (3)
- ruby on rails (10)
- usability (11)
- user experience (5)
- ux week (17)
- yak (7)
- yui (4)