nl2br in RoR
I recently found out that line breaks are not recognized when displaying a large block of text on the view (e.g. a post body). In PHP this was handled by the nl2br method but I had no luck finding a similar function in RoR. So I did what any sane person would do, create one myself. I placed this method in our model to format a particular field but it seems a better choice to put it in a helper.
def nl2br(text)
return text.gsub(/\n/, ‘<br/>’)
end
And there you have it, your very own nl2br method. Either you just found something really useful or I just wasted 2 minutes of your time. If anyone has a better solution or if I missed a function for this let us know by dropping a comment.
Small But Terrible
I’m pretty sure most of us have been in a situation where we have been trying to fix a bug for hours and finally seeing that there had just been some tiny typo or maybe even a commented line of code. I’ve been through a lot of those experiences before, especially when I was still learning to code, and most of the time, I just breathe out a huge sigh of relied or just plainly laugh at myself.
Reminiscing about those ‘good old days’ could be fun too. Here’s some of those ‘what the hell/oh come on/I knew it was just a simple mistake!’ scenarios that I have experienced or just know of:
- A missing/extra space, comma, or whatever character in your code. When this happens, it usually takes a while before you realize it because most of the time, you assume that what you typed was correct. Especially with missing spaces, it really is hard to tell. I have had one experience before in coding in Flash, where my fellow coder had 3 extra spaces after the instance name of a clip. No wonder we couldn’t access the variable!
- Unpopulated database. I think Bit had experienced this too, when he was launching the AnimoAteneo.com site. I have also experienced this recently, where I KNEW my code was correct, but the reason why it wasn’t printing out anything was because it didn’t select anything(I was trying to select events during a week, which was stored in database…and the problem was there were no events that week).
- The semi-colon of emptiness. There are rare times in the life of a programmer(especially ones that still use the ancient arts of Java and other compiled languages) when s/he encounters the…wait for it…semi-colon of emptiness(SFX: thunder crash). This is one of the most frustrating things that can happen to you: accidentally put a semi-colon after an if statement. Although it has been used deliberately for empty if statements, some still accidentally put a semi-colon after the if statement, which invariably makes it useless. And for that, we have to thank those other languages that don’t require a semi-colon.
- HTML entities. These guys are a nightmare, especially with a database involved. tick-marks or whatever you call it are often confused with apostrophes, tabs(nbsps) with spaces, long dashes with short dashes(sounds weird but it does happen), etc. It is a real pain especially when the program or database doesn’t complain about it and you just find out about it after they have wreaked havoc in whatever you were working on.
- Uncommented/commented lines of code. This happens especially when you are using the “try and try ’til it works” approach in coding. This also happens when you are trying to debug your code and you forget to uncomment/comment some lines that subtly affect whatever your code was doing – like, say – assigning a static/hard-coded value to a variable.
These are just some of the mistakes that we can encounter while coding and minimizing the chances of these happening can help in coding a lot faster and having more time dealing with bugs that matter.
January 2008 PhRUG Meet-up
A PhRUG meet-up was held on Jan 16, 2008 at Ateneo de Manila University sponsored by Admoo Labs. 30 people attended the event. I gave a presentation on ‘Website Development with Ruby on Rails’. Greg Moreno talked about RSpec.You can download a copy of my presentation here. Pictures are here.
Hello World.
Hello, world. We are Admoo Labs!
We’re a group of four young men trying to start something big while starting small and this blog is where you’ll find our “story”. My colleagues, Topher, Gerald, and Eumir, will occasionally drop by here too and share their thoughts on web development, startups, business, and life in general.
Stay tuned, folks. I may just be being my usual optimistic self, but I think this is the start of something wonderful.