SessionlessBots

It is based on the idea that bots shouldn’t be assigned any session variables. I mean, what good do those bots make out of those sessions! Pfft, so why should we assign sessions to them? Hell if we use a database as a session store, it’ll just keep eating them up!

No more! Say it with me. No more! We will not be controlled by those pesky bots any longer!

Installation

Installing SessionlessBots is just like installing any other plugin:

./script/plugin install git://github.com/aizatto/sessionless_bots.git

License

SessionlessBots is released under the MIT license.

Note: This is just an old plugin I put together aeons ago. Thought I’d at least create a page for it on my site.

This code was based off of Obie Fernandez’s Wrestling with the Bots

Posted in SessionlessBots | Leave a comment

Building Custom Error Pages

A good error page is important in the user experience of a website. Lucky for us Ruby on Rails provides a convenient way to update any 404 (Not Found), 422 (Unprocessable Entity), and 500 (Internal Server Error) errors that occur in your application.

This is easily done by updating the files located in your Rails root at:

  • /public/404.html
  • /public/422.html
  • /public/500.html

But sadly, it’s not good enough.

The Problems

The problem with is that it simply isn’t flexible enough:

  • Unable to embed ruby code.
  • Requires duplicating layout code.
  • Dynamic code in layout is made static in error pages.

The Solution

Inside the ActionPack code, located in /lib/action_contorller/rescue.rb File is the method rescue_action_in_public.

Lets have a look at the comment:

Overwrite to implement public exception handling (for requests answering false to local_request?). By default will call render_optional_error_file. Override this method to provide more user friendly error messages.

Bingo! Exactly what we wanted.

Firstly lets generate he simple 404, 422, and 500 error pages. Likewise I leave them in their own directory in the /app/views File folder, I like to call this directory “errors“.

Hence my new files will be located like so:

  • /app/view/errors/404.html.erb
  • /app/view/errors/422.html.erb
  • /app/view/errors/500.html.erb

Now feel free to remove the error pages in your /public File directory.

Now in our ApplicationController (see /app/controllers/application.rb File ), lets add override the function rescue_optional_error_file,

Note: I have to explicitly state the file extension and layout, else when an unknown format is requested

Testing

Off the bat, you cannot test this on your local machine. You are required to redefine the local_request? in the ApplicationController, like so:

Now you are ready for testing out your new error pages!

Note: Don’t forget to remove the local_request? when done!

Posted in Handling Errors | 2 Comments

Filter Listings of an Object’s Methods in IRB

So the next stop after sorting an object’s methods in irb, is to be able to filter it.

Why? Well I roughly know the method name, so how about filtering down that big list of 163 methods names to something smaller? like say 5?

The traditional way to do it is (yes without the change in my last post):

object.methods.sort.select { |a| a =~ /rev/ }

Damn that’s one long line. Even with my change, it reduces it by only 5 characters.

So what is one to do? Try popping this into your ~/.irbrc:

Now we can do this:

object.methods :rev

The World is Just Awesome.

Posted in Open Classes, Tips, irb | Leave a comment