Hi! Thanks for visiting my blog. If you've received any value from my content would you mind supporting my new startup by downloading our browser add-on? It's called PriceBlink and makes online shopping a breeze. You can watch it in action here and download it for Chrome, Firefox, IE, or Safari by going to PriceBlink.com. Thank you and I hope you enjoy!

Ruby on Rails Replacing Line Feed with HTML Equivalent

Aug 31

This is a Ruby snippet that’s simple and something that I’ve found useful. When allowing users to enter multiline data in your web application, say through a textarea, there will most likely be line feeds. When a user hits the enter key they will expect the cursor to move to the next line. This is default textarea behavior, but what happens when this is stored to the database and then displayed on the a web page?

In most instances the database will save the line feed as the \n character. As you most likely know, the web browser will ignore this line feed. So a simple fix is to replace line feeds with their HTML equivalents. The following snippet will do the trick:

@body.gsub(“/n”, “[br /] “);

Read More

Ruby’s GemServer

Aug 29

If you’re doing Rails development I came across a handy utility that I’d like to share.  It’s the RubyGems documentation that can be easily accessed on your local machine.  Simply run the following command at your command prompt:

gem_server

and an instance of WEBrick will start on port 8808.  Accessing this through http://localhost:8808 will display an index page (shown below) with all gems installed as well as their documentation.  I’ve found this useful in several instances where I was away from an internet connection and needed access to the RDocs.  Enjoy!

Read More

Truncating Text with Ruby on Rails

Aug 27

Truncating text is a very handy utility when building web applications. This can come into play when displaying part of a blog post, forum post, comments, and so on. Luckily, Ruby on Rails has a nifty built-in helper method, intuitively named truncate. Let’s say you have the following string:

Hello, this is my first blog post!

and let’s say you’ve assigned it to a variable called @post. In you view template you could then truncate using:

truncate @post, 20

This would yield:

Hello, this is my…

If you’re so inclined to count the characters you’ll realize there’s only seventeen with “…” accounting for the last three. This can be fixed by specifying a third parameter to the truncate method:

truncate @post, 20, “”

The third parameter is actually a custom string that you can tell Ruby to append to your truncated string. Just remember that it actually counts in your total character count so take this into consideration. Here’s an example:

truncate @post, 23, “!!!”

which yields

Hello, this is my fi!!!

Personally, I don’t feel that three characters cause that big of a difference when dealing with something as inexact as truncating text. Yet, if the situation calls for exactness then be sure to take this into consideration.

Read More

ActionMailer on Bluehost

Aug 21

I ran into my first MINOR problem with Bluehost this evening.  It’s more of a Rails configuration problem than anything else.  My ActionMailer class kept throwing the following error:

Errno::ECONNREFUSED (Connection refused – connect(2)):

In my environment.rb file I was using a fully qualified domain name for my ActionMailer settings.  I decided to try localhost since I wanted to relay mail through the local machine that my app was running on.  Everything worked perfectly and the config now looks like:

ActionMailer::Base.server_settings = {
:address  => “localhost”,
:port  => 25
}

Give that a try regardless of hosting provider if you’re receiving the error message mentioned above.

Read More

Farewell GoDaddy, Hello Bluehost

Aug 20

GoDaddy, you have been a royal thorn in my side. I can’t count the number of wasted hours I spent configuring and hacking away to get my Rails app working on your servers. That doesn’t include the numerous blog posts I wrote to help others get up and running with your troublesome service. I’d be willing to forego all the configuration problems if my application speed was acceptable, but it’s not….it’s far from it. When I say acceptable I’m not asking for the world, just response times < 5 seconds with VERY light pages. You still rock for domain registrations....hands down.

With that being said I'm making the switch to Bluehost. A good friend pointed them out to me and it appears they’re rated #1 by the Rails developer community. I’ve only been with Bluehost for two hours now and haven’t run into any major problems. They have a good Rails howto that I’m looking forward to implementing painlessly. The fact that the Bluehost CEO has a blog helped seal the deal for me. This makes the company much more personable and approachable.

Read More