Trey Connell

January 15, 2011 | In: Ruby on Rails

Basic HTTP Auth for Rails

Usually I use .htaccess to lock down a dev site or something I don’t want the world to see. This morning I had to do it for a Rails app and didn’t have the luxury of using .htaccess so this is what I did as a quick and dirty solution.

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :authenticate

  USERNAME, PASSWORD = "woo", "hoo!"

private

  def authenticate
    authenticate_or_request_with_http_basic do |user_name, password|
      user_name == USERNAME && password == PASSWORD
    end
  end

end

Now when I'm ready I just have to remove that filter.

Here’s a helper method I wrote in order to resize an image for display. And yes, you should always try to physically resize an image and not just change the height and width in the image tag so that you save space and bandwidth and load time. However, I ran into a situation where this was not an option, but I still had to apply a maximum height and width to a particular image.

  def resize_image_dimensions(max_height, max_width, height, width)
    if height > max_height
      ratio = max_height.to_f / height.to_f
      height = max_height
      width = (width * ratio).round
    end

    if width > max_width
      ratio = max_width.to_f / width.to_f
      width = max_width
      height = (height * ratio).round
    end

    [height, width]
  end

I then use the method like this in my view:

<% height, width = *resize_image_dimensions(118, 118, @image.height, @image.width) %>
<%= image_tag @image.url, :width => width, :height => height, :border => 0, :alt => @image.alt_text) %>

I typically have the following code in my application_controller.rb file in Rails apps:

# any time we get a RecordNotFound Exception we're going to rescue from it and throw a 404
rescue_from ActiveRecord::RecordNotFound, :with => :throw_404

# Used to take the user to a 404 page
def throw_404
  @browser_title = "Page Not Found"
  render_optional_error_file("404")
  true
end

You probably already know that ActiveRecord’s find method will raise a RecordNotFound exception if no result is returned. However, when you use ActiveRecord’s ‘find_by_*’ methods in your Rails app, you’ll find that RecordNotFound Exceptions are not automatically raised for you. This causes you to have to do something like this:

@foo = Foo.find_by_name('bar')
raise ActiveRecord::RecordNotFound unless @foo

To avoid this, you can simply use the ‘bang’ version of the ‘find_by_*’ methods. So the code above simply becomes:

@foo = Foo.find_by_name!('bar')

If bar is not found then the RecordNotFound exception will be raised.

Credit: Randy Schmidt

I had to modify an application to ensure the https protocol was used on some important pages such as new user registration, login, shopping cart checkout, etc.  Here’s a really easy method you can place into your application_controller.rb and call as a before_filter within your controllers.

def require_ssl
    redirect_to :protocol => "https://" unless (request.ssl? or RAILS_ENV != 'production')
end

October 19, 2010 | In: Fatherhood, Life

It’s the Dad Life

A fellow dad and buddy of mine showed me this video a few weeks ago, and I could not stop watching it. These guys do an amazing job of putting this rap video together describing how WE roll. Enjoy!

September 29, 2010 | In: Life

I Miss Mitch

I’m sick of following my dreams, man. I’m just gonna ask where they’re going and hook up with them later.

– Mitch Hedberg

August 8, 2010 | In: Development

Tracks and Basecamp

I started working today on integrating Tracks and Basecamp for the heck of it. So far it’s coming together pretty well. You can check out the latest where I forked Tracks on github.com. The project integration is done, and to-do lists are up next. Shouldn’t take much longer. I’m not sure exactly how useful it will be at this point – but hey – I’m adding options for folks using Tracks. And it’s fun!

Mislav posted some really good tips for using Git.

http://mislav.uniqpath.com/2010/07/git-tips/

July 23, 2010 | In: Life, Suckage

Trying to Relax

I’m sitting at the Lake of the Ozarks in a condo that we rented with some friends. My daughter is with her grandmother. My wife is out shopping. My buddy is playing Red Dead Redemption on his PS3. And what am I doing? I’m CODING.

I swear I’m incapable of letting my brain just check out and do nothing. I can’t help but think about all the items on my plate and what work is going to be like when I return next week. Sometimes I wonder if vacation is even worth it.

This game looks really bad-ass. Everyone loves to use the sniper rifle no matter what game you’re playing. Ghost Warrior looks like it’s going to be nothing but sniper rifle action the entire time!

My daughter just turned 18 months old yesterday. If there’s one thing I’ve learned, it’s that losing your temper and raising your voice in an effort to force her to obey is akin to kicking a boulder as hard as you can – without shoes on. Like that boulder, my daughter just doesn’t care.

In the months leading up to Addison’s birth, I thought many times about the types of discipline we would use and how I wanted to be one of those “I’m your Father not your friend” parents. I thought the stern, raised voice and mean looks were valid disciplinary actions and an effective means of modifying your child’s behavior. Boy, was I ever wrong.

After about 15 months, I realized I was getting nowhere. My daughter still got into the same things I’d tried to keep her out of since she could crawl. She still went right off to the next off-limits area as soon as I yelled at her. It’s like “big bear daddy” didn’t phase her at all.

One day about 3 months ago I decided to go the exact opposite.  If she did something bad, I would simply shake my head no at her and move her away from whatever she was doing (or whatever it was away from her). After a couple of weeks I noticed her looking at me first before grabbing things like my iPhone or the cat’s tail. She was beginning to realize that what she was doing was wrong, and she was checking with me to see if anything had changed since the last time. Simultaneously, I started being over-the-top congratulatory for anything she did well. Throw your garbage in the trash can? Woohoo!!! Put your dirty socks in the laundry basket? Jumping around and shouts of happiness! Leave Daddy’s iPhone where it is when you point at it and I shake my head? Hugs and kisses all around!

And you know what? It works. I mean, it really works. She now behaves better than ever, and while she’s not perfect (and no child is) she is respectful of Daddy and wants to please me more than she wants to grab the iPhone.

This is just a bit of new Dad information I thought I might share with any of you who, like me, think yelling and being a big bad wolf is the best way to get your child’s attention.

Tradeomics

Twitterings

Recent Comments

Buy Awesome T-Shirts!

I hate pants t-shirt