Ruby on Rails Cheat Sheet Collectors Edition

This Ruby on Rails cheat sheet is a reference guide for RubyOnRailsBlog.com that will be continuosly updated and improved as new features are added to rails and new cheat sheet tips are added to the guide. It is a collection from the one of the very best guides, apis and source snippets that are concise that i’ve found. I’ve fixed errors and added highlighting to it and it is a mirror of the one found on nanorails site plus my own changes. New updates will be posted to this post under Recent Updates.

Common Error With Flash Notices

I recently was asked to look at an app where none of the css flash generated errors were being displayed. Low and behold the culprit was something very simple in the syntax involving ruby symbols.

Their Problem

flash['notice'] = "Section Created Successfully"

My Solution

flash[:notice] = 'Section was successfully created.'

Disable Submit Button After its Pressed

Ever run into a problem where someone hits your apps submit button multiple times and it throws the app into a frenzy?

One solution for this would be to disable the submit button after it has been pressed. Ajax concepts are definetely maturing and i'd like to see more time spent simplifying these types of actions in rails. One of my qualms is that it is complicated to tie multiple buttons, their onclick actions and validation behind the scenes into one simplified process.

One thing to note is this seems to have errors in IE.

Use the Ruby Captcha Gem for Rails User Registration

If you decide that you want to add captcha validation to the user registration of your ruby on rails application its really not that difficult. Make sure first to install the gem using gem install captcha.

After installing the gem you can use / modify the following code to add captcha protection to your user signups:

Rails Expiration of Sessions due to Inactivity

In some apps (some may argue most) session management is a critical aspect for performance and control of the system. From a sysadmin point of view it doesnt make a lot of sense to let a session stay connected forever since it will hog resources that could be put elsewhere.

So here is a way to expire sessions that become inactive in rails:

Posting Code In Comments

I fixed the problems people were having posting code in comments. You should be able to use coderay now and post your code using the html tags like so:

<typo:coderay lang="ruby">

class RubyCodeTest
  def rails
    "is fun"
  end
end

</typo:coderay>

Enjoy!

Ruby on Rails Blog Live Ajax Comments Previewing

It seemed strange to me that there was no default asynchronous comments on this web spiffy blog and I was never a huge fan of preview buttons myself so I hacked in a live preview! Go ahead and give it a whirl. This very simple code can be used for far more then the blog app typo that runs this site. It can observe any form you have on your sites or projects.

To implement it an your own blog, edit app/views/articles/_comment_box.rhtml and do the following: