Most of your controller methods are public actions that display pages on your website. Your app’s routes will point to these methods and show the corresponding page (view). For example:

class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  def show
    @post = Post.find(params[:id])
  end

end

But hopefully you are keeping things modular and breaking up complexity into smaller methods. For example, you might want to perform some check before you allow the user to add a new post:

class PostsController < ApplicationController
  def new
    check_something
    @post = Post.new
  end

  def show
    @post = Post.find(params[:id])
  end

  def check_something
    redirect_to(root_path) and return if something
  end
end

This is great. BUT, you should be sure to make the check_something method private and make it accessible by defining it as a helper method:

class PostsController < ApplicationController
  helper_method :check_something

  def new
    check_something
    @post = Post.new
  end

  def show
    @post = Post.find(params[:id])
  end

private

  def check_something
    redirect_to(root_path) and return if something
  end
end

Why Make the Helper Methods Private?

So, why is it important to make methods like this private? Because you don’t want them to be accessible to your users. They can’t hit that method unless you have defined a route that points to it as an action. While unlikely, it’s possible that you might have such a route defined. Or, if you have some type of catch-all route defined, then this check_something method could actually be accessed by a user.

In most cases, this will be unlikely to happen. And if it does happen, it’s likely that little harm could be done. However, making these non-action methods privates also makes your code a bit more clear by declaring that these methods are NOT accessible via any routes. So, simply put, you should make the non-action methods private because it’s a best practice.

{ 0 comments }

When googling to find the answer to a ruby (or rails) coding problem, it struck me that code snippets always use “require” to include any necessary libraries. For example, I recently searched for a way to compare IP addresses using CIDR notation. (I had IP ranges stored in CIDR notation and wanted to see if the user’s IP was in the range.)

After hunting around, I came up with this solution:


require 'ipaddr'

admin_ip = IPAddr.new('192.168.1.0/24')
client_ip = IPAddr.new('192.168.1.2')
admin_ip.include?client_ip => true

# Do it in one line:
IPAddr.new('192.168.2.0/24').include?IPAddr.new('192.168.2.100') => true

Using require in this code snippet is clear and it makes lots of sense to show it this way. You can see immediately that the code is dependent on the ruby IPAddr library. But in real code, you should never use require to include libraries or other files.

Best Practice: Use Autoload Instead of Require

Instead of using require, you should use autoload. And furthermore, you should never put the autoload statement in middle of your code, as might implied by typical snippets. Includes should always happen at the top of any file. For example, looking at a ruby file some_class.rb:


autoload :IPAddr, 'ipaddr'

class SomeClass

    # returns true if ip_check is contained in ip_master range (CIDR notation)
    def check_ip(ip_master,ip_check)
        IPAddr.new(ip_master).include?IPAddr.new(ip_check)
    end
end

Why use Autoload instead of Require?

So, why use autoload instead of require? Because require loads the file immediately when ruby processes the require line. Autoload, on the other hand, doesn’t load the file until you try to access the symbol defined in the autoload statement. If you don’t accesses it, it doesn’t get loaded and you eliminate any unnecessary overhead processing.

But autoload uses require under the hood. So just like require, autoloaded files are only included once. By comparison, using ‘include’ would load and include the file every time.

Why Put Autoload at the top of the file?

This is just a best practice. You don’t want to hide dependencies buried deep within a method. When I open a file, I want to see the dependencies right away. This makes code maintenance and debugging much less error prone – especially if you are working on a project with multiple engineers or returning code you wrote last year.

{ 0 comments }

How to test IE9, IE8 and IE7 from the same computer

windows

I’m a software developer and I often need to make sure that a web page or site will display correctly in multiple versions of Internet Explorer. Specifically, I need to test a site in Internet Explorer 7 (IE7), Internet Explorer 8 (IE8) and Internet Explorer 9 (IE9). When I installed IE9, I of course lost [...]

Read the full article →

Windows 7 Can’t Connect to Default Administrative Share C$

networking

The Problem: Windows 7 Fails to Connect to Default Windows Admin Shares on Networked Drives I got a new laptop with Windows 7 and I found I suddenly could not connect to the default administrative shares on other networked windows machines. For example: using \\remoteComputerName\C$ to connect to the default C drive admin share on [...]

Read the full article →

Firefox dropdown menus are flickering on 2nd monitor

firefox shortcuts, tweaks, and tips

The Problem: Firefox Menus Flicker and Go Blank Had a very weird problem with Firefox on my Windows 7 box. (It might also happen on Ubuntu desktop.) I switched the connection on my second, extended desktop monitor to use DVI instead of VGA. Suddenly, all the Firefox drop-down menus went blank. They would flicker briefly [...]

Read the full article →

Setting Ubunutu 10.04 Raid with Dell PowerEdge T310 and PERC S100

file commands

It turns out that Dell PowerEdge servers (like the T310) with PERC S100 do not support RAID on Ubuntu. Bummer. But here is how I got Ubunutu software RAID 1 to work with my Dell PowerEdge T310, PERC S100 and Ubuntu 10.04 LTS… The main trick was to turn re-initialize the PERC Controller to use [...]

Read the full article →

How to view and debug PHP arrays

debug

Sometimes when coding or debugging in PHP, it’s helpful to see the entire contents of an array. An easy way to do this is with the print_r command. To make it more readable, wrap it in <pre> tags. For example, to see the contents of associative array $FormData: You’ll see output like this: Array ( [...]

Read the full article →

Setup WordPress or WPMU to make an atomic version switch — AND allow you to revert

apache

I have a new WordPress MU (WPMU) install and I am ready for my first upgrade. I couldn’t get automatic upgrade to work, and all the forums said: do it by hand manually. This is fine, but I didn’t want my site to be in flux with some old an some new files as I [...]

Read the full article →

How to keep the existing file attributes (owner, timestamp, etc) when copying files or directories

command line

When copying files and especially directories, sometimes you want to keep the existing file attributes. For example, you may likely want to keep the same owner, group, timestamp, etc. You can keep the attributes by using the preserve argument. preserve=all will keep everything: You can use the -p version of preserve to keep the default [...]

Read the full article →

How to Check the Ubuntu Version.

command line

How do you know which version of Ubuntu you are running? This will return something like this:

Read the full article →