Monthly Archives: January 2008

Send email in Ruby on Rails Use mail_to tag instead of link_to!

I know there’s more than a few of you who have been using link_to for email links. No more! By using mail_to, you can create a mailto link tag to the specified email address, which is also used as the name of the link unless you specify otherwise. 

An added beneift is that mail_to can accept an encoded hex or javascript parameter, thereby encoding your email addresses to codevent email harvesters. Another thing you’ll notice is that link_to and mail_to are reversed:

# using link_to
link_to "Email Jonathan", "mailto:hello@jonathansng.com"

# using mail_to
# note the reversal
mail_to "hello@jonathansng.com", "Email Jonathan"

# using mail_to with :encode => "hex"
# returns <a href="mailto:%68%65%6c%6c%6f@%6a%6f%6e%61%74%68%61%6e%73%6e%67.%63%6f%6d">Email Jonathan</a>
mail_to "hello@jonathansng.com", "Email Jonathan", :encode => "hex"

Read up on mail_to

SEO friendly URLs in Rails with friendly_id, goodbye permalink_fu!

SEO friendly URLs can be accomplished through various methods in Rails. Most of the time I have been using permalink_fu for most of my URL rewrites. Recently, my friend published a great replacement to permalink_fu called friendly_id. 

http://www.jonathansng.com/blog/1

will automagically become

http://www.jonathansng.com/blog/why_this_plugin_rocks

As an added benefit, if you already have permalinks in play friendly_id will 301 redirect incoming requests so you don’t get dinged!

Check out friendly_id over at agilewebdevelopment.com or at his site

rake db:reset in Rails 2.0

Migration are great! Initially, I was a bit confused with their usage, but the first time I wanted to revert back to a prior database schema…their existence was vividly clear.

Sometimes migrations randomly break, and I’m sure there is a good reason why they do; however, I’ve never really wanted to dive into the issue. Maybe it’s just me, or maybe other people have been doing the same thing I have been doing…all these months I’ve been manually dropping my databases every time I’ve wanted to recreate them.

Let me introduce a handly little line that will drop all your tables, and rebuild them to the most current schema!

rake db:reset

Model, View, Controller by James Dempsey


I found this little gem on YouTube the other day, after poking around a bit I found actually lyrics!

Model, View, Controller

Lyrics and music by James Dempsey.

Model View, Model View, Model View Controller
MVC’s the paradigm for factoring your code,
into functional segments so your brain does not explode.
To achieve reusability you gotta keep those boundaries clean,
Model on the one side, View on the other, the Controller’s in between.

Model View - It’s got three layers like Oreos do.
Model View creamy Controller

Model objects represent your applications raison d’tre.
Custom classes that contain data logic and et cetra.
You create custom classes in your app’s problem domain,
then you can choose to reuse them with all the views,
but the model objects stay the same.

You can model a throttle in a manifold,
Model level two year old.
Model a bottle of fine Chardonnay.
Model all the twaddle stuff people say.
Model the coddle in a boiling eggs.
Model the waddle in Hexley’s legs.

One, two, three, four.
Model View - You can model all the models that pose for GQ.
Model View Controller

View objects tend to be controls that view and edit,
Cocoa’s got a lot of those, well written to its credit.
Take an NSTextView, hand it any old Unicode string,
the user interacts with it, it can hold most anything.
But the view don’t knows about the Model:
That string could be a phone number or the words of Aristotle.
Keep the coupling loose and so achieve a massive level of reuse.

Model View - All rendered very nicely in Aqua blue
Model View Controller

You’re probably wondering now.
You’re probably wondering how,
the data flows between Model and View.
The Controller has to mediate,
between each layer’s changing state,
to synchronize the data of the two.
It pulls and pushes every changed value.
Yeah.

Model View - mad props to the smalltalk crew!
for Model View Controller

Model View - it’s pronouced Oh Oh not Uh Uh
Model View Controller

There’s a bit more on this story,
a few more miles upon this road,
well nobody seems to get much glory
writing controller code.
Well the model is mission critical
and gorgeous is the view,
But I’m not being lazy, but sometimes it’s just crazy
how much code i write is just glue.
And it wouldn’t be so tragic,
but the code ain’t doing magic:
it’s just moving values through.
And I wish I had a dime
for every single time
I set a TextField’s stringValue.

Model View - how we’re gonna deep-six all that glue
Model View Controller

Controller’s know the Model and View very
uahh - intimately
They often are hardcoding
which is very verboten for reusability.
But now you can connect any value you select
to any view property.
And I think you’ll start binding,
then you’ll be finding less code in your source tree.
Yeah I know I was astounded,
that’s not even a rhyme.

But I think it bares repeating
all the code you won’t be needing,
when you hook it up in IB.

Model View - it even handles multiple selections too
Model View Controller

Model View - hope I get my G5 before you
Model View Controller

Yeah, yeah, yeah. Yeah.

Image submit button tag for Rails

Want to replace your form submit button with an image? Boom.

<%= form_tag :action => :update_user, :id => user %>
<%= image_submit_tag "IMAGE_URL", :class => 'CLASS_NAME' %>
<%= end_form_tag %>

# will generate:
# <form action="/update_user/1" method="post">
# <input class="CLASS_NAME" src="/images/IMAGE_URL" type="image" />
# </form>

WA State Enhanced Driver License ID Card (EDL/ID)

I was listening to NPR earlier today and heard that WA state will be one of the first states to voluntarily offer an enhanced ID card. If you’re traveling to Canada, it looks like saying “I’m an American!” won’t be enough to get back into the country. A US Passport or the new WA enhanced ID card will be acceptable. The enhanced drivers license (EDL) looks like it’ll cost $15.00 more than a regular WA drivers license.

Want to find some more information on the EDL/ID?

Basic Ruby idioms for Ruby on Rails

I was chatting with a few friends who as of late, have had an increasing interest in Rails. All of them want to learn RoR for different reasons, but although their reasons for learning are different they seem to have a set of common questions. I thought I’d put together a post with a few common idioms that have been irking them.

empty!
The “bang” method can be attached to the end of other methods. These methods often indicate something destructive…bang! @class.destroy!

empty?
The “predicate” methods will return a true or false. @class.empty?

a || b
This expression first evaluates a, if it isn’t false or nil or false then ruby stops evaluating the expression and returns a. If it is false or nil, it will return b. I use this to return a default value if the first value hasn’t been set (read: page title).

a ||= b
Assign a the value of b if a doesn’t already have a value. So, count ||= 0 will set count to 0, if count doesn’t already have a value.

count += 1
In PHP, we’d use i++ but in Ruby, we use this syntax.

JMI Motoring in Smokey Point does great work!

I dropped my B6 S4 off last week to have some body pieces painted and installed. I thought, hell, while I’m at it maybe I can have them paint my race bike body work the same color. I worked with Jeff, he’s a great guy! He  painted and installed my side skirts, blacked out my front grills, and painted my race bodywork. I could not be happier.

Before:

After:

Check them out if you need anything done!