Tips 'n Tricks

Backup your data *now*, damnit!

Tags: 

Nothing scares you into realizing how fragile our data is until you accidentally delete something important.

In my case it was trying to get some pictures off my phone; the bluetooth connection was misbehaving so I figured I'd just pop out the memory card, only it wasn't working in the card reader, so I put it back in the camera to try the bluetooth again when sccccrrrraaaaapppe! it scratches against the slot and gets jammed. I had to pry the card out and when it did it had two small but visible scratches across it, and for a teeny-tiny MicroSD card even small scratches appear to make a difference. I plugged the card back in, carefully this time, but it wouldn't work. I rebooted the phone in case that might wake it up - nothing! I'd just lost about 30 pictures of the family and half a dozen movie clips including some utterly adorable ones.

So let this be a lesson to all of you - back up your data, and don't fool around with important files if you a) don't have backups and b) aren't paying attention to what you're doing.

Insanely easy tabbed web pages with Prototype

Tags: 

I came across this today when trying to find an easier way to present a six-part form in a Rails project - a tabbed interface system built upon Prototype (an integral part of Rails). Called Control.Tabs it makes tabbed interfaces a synch to do: simply add an unordered list containing links to the blocks containing your to-be-tabbed content, and add a line of JavaScript to start the ball rolling - that's it! Some of the examples given on the site take it even further, including building an image gallery, and I've used it to build a series of form pages into one page complete with back/forwards buttons, all without refreshing the page. Good stuff, and well worth the time for any Rails developer to try using.

Making a form reset button in Rails

Tags: 

Rails doesn't have reset_tag to automatically make a form reset button, instead you have to take a slightly different route. To make a form reset button in Rails you simply do this:
[source:ruby]
'reset', :id => 'reset_button', :type => "reset" }) %>
[/source]
which will create the following:
[source:html]

[/source]
The important part here is the :type argument, which override's Rails' default of "submit" for the submit_tag. Then, to tidy it up a little I override the :name, which defaults to "commit", and give it a useful id attribute.

Et voila!

Strip HTML in any string in Ruby

Tags: 

A little code snippet to let you remove HTML from any string in Ruby:
[source:ruby]
class String

# remove all HTML tags from the source string
def strip_html
self.gsub(/]*>/, "")
end

end
[/source]
Then to run the code just do this:
[source:ruby]
blurb_html = "

Thanks for visiting our site.

"
blurb_text = blurb_html.strip_html
[/source]

Pages

Subscribe to Tips 'n Tricks