Tuesday, September 13, 2011

How to display an error message to the user, just like ActiveRecord's validators

The default validation handling of the activerecord validators is pretty nifty, in that it automatically does so much for you, like giving the view messages to display to the user, automatically saving user input in the form, etc. Here's how to manually throw an error that functions just like the validator errors.

In your model:

before_validation do
  if something
    self.errors.add(:some_field, "already exists")
  end
end

If you have the appropriate setup in your rails form to display the error messages:

<% if @model.errors.any? %>
  <% @model.errors.full_messages.each do |msg| %>
    <%= msg %>
  <% end %>
<% end %>

Then when you do an @model.save with invalid data, your view will automatically display the following to the user:
"some_field already exists"

To simply throw something that halts execution, do a:
raise "something here"

Then you'd get a full stop error message and stack trace.

Monday, August 22, 2011

Setting up your work environment with automator

I frequently find myself having to open the same set of applications in order to work (textmate, terminal with several tabs, browser, etc.). So I figured I'd spend some time figuring out how to automate the process.


  • open automator
  • create a new application
  • find "run shell script" <-- try it with this one first, if it doesn't work, use "run applescript"
  • input the following into the shell scripting area (it's a rather small textarea). Change the settings for your particular set of applications you need to use. For me, I want to open my default browser to my local app, open textmate with my project, open my text file with my ongoing notes, and open several terminal tabs with various commands already entered.


open http://myapp.localhost
open ~/Work/myapp/myapp.tmproj
open ~/Desktop/myapp.rtf

osascript 2>/dev/null << EOF

    tell application "Terminal"
       activate
    end tell
    tell application "system events" to tell process "Terminal" to keystroke "t" using command down
    tell application "system events" to tell process "Terminal" to keystroke "t" using command down
    tell application "system events" to tell process "Terminal" to keystroke "t" using command down
    tell application "Terminal"
        do script with command "cd ~/Work/myapp; rails console" in first tab of first window
        do script with command "cd ~/Work/myapp/log; tail -f development.log" in second tab of first window
        do script with command "cd ~/Work/myapp; su -" in last tab of first window
    end tell
EOF

  • save it and put it in your applications directory (it doesn't matter where you put it, but I figure this is the best place)
  • then drag it to your dock

Saturday, July 9, 2011

Installing PostgreSQL on OSX

I've been playing around a bit with PostgreSQL on OSX, and I had to use a bit of *nix/bash general knowledge to get it working, hopefully this can save someone else a little time.

After running the installer that you can find here: http://www.postgresql.org/download/macosx
  1. Switch to root ( su - ).
  2. Then su - postgresql (the installer creates a postgresql user on your box, but you can't login directly as that user, except through root since su - as root bypasses password checks).
  3. Go to your bin directory for PostgreSQL (default on OSX through the install wizard is /Library/PostgreSQL/9.0/bin) and run ./createdb -s -P yourusername (I used my default OSX user). It will prompt you for a password that you want to use for this user. I used my default OSX user's password. One less thing to remember.
  4. Exit back to your default user.
  5. In your .bash_profile, add this to your PATH variable :/Library/PostgreSQL/9.0/bin (change this if you installed to some other directory).
  6. Be sure to run a source ~/.bash_profile so this variable takes effect in your current terminal session.
Now your default OSX user can run PostgreSQL commands.

Tuesday, May 17, 2011

OpenSuSE and mod_proxy_html

Dear OpenSuSE,

What is the point of having mod_proxy_http but not mod_proxy_html installed by default for httpd? This means that out of the box, httpd on OpenSuSE can't run a reverse proxy. Why did you do this?


Thanks,
Branden


Tuesday, April 12, 2011

$.parseJSON with Rails 3's .to_json

This one stumped me for a bit today. If you had any old code from Rails 2.3.* and converted it to Rails 3.0.*, watch out for any instance variables in your controller that you call to_json on for processing by jQuery's $.parseJSON in your view.

Because the default in Rails 3 is to make all strings html safe, you no longer need to use the "h" helper method in your views. However, this means that code like this will no longer work:

controller: @temp = {:hello =>; "world"}
view: alert($.parseJSON("<%= @temp.to_json %>"))

You will get invalid JSON errors, because the "safe" string is not in a valid format for parseJSON. To solve this, use the Rails 3 html_safe helper to tell the view that this string is ok to send through as is:

controller: @temp = {:hello =>; "world"}
view: alert($.parseJSON("<%= @temp.to_json.html_safe %>"))