Dec 27, 2011

More on Twitter Bootstrap in Rails


Yesterday I wrote about error handling in Rails while using Twitter Bootstrap.  One of the interesting things I was able to do with the custom form builder was move the active record attribute errors to the input fields they're associated with.  The example below is a bit simple but it should get the point across....




If you look at the template that created the form below you can see that that it doesn't include any special functionality.  All the work happens in the form builder.





If you look at the "text_field" method in the code below you can see that it just calls "input_wrapper" to add some html around the "text_field".  Since "input_wrapper" already had to test each active record attribute for errors so that it could add "error" to the classes of those elements it was pretty straight forward to tack an inline-help span on the end.





The code above is not elegant, it's just an experiment.  I certainly don't recommend using it as is but I wanted to share just in case some one is thinking about similar problems.


In general though I am fairly happy with the separation of concerns so far.  I think that by treating the multi-element fields bootstrap expects in it's forms as primitives I will be able keep my templates focused on the content and my presenters on the logic of the view.

Dec 26, 2011

Formatting Rails Errors for Twitter Bootstrap

I'm using Twitter's Bootstrap toolkit for the layout in a proof of concept application I'm working on.  So far my general impression is that I really like Bootstrap but there are a few obstacles to making it play nice with Rails.

One of those obstacles is that Rails formats fields with errors quite a bit differently than Bootstrap's CSS expects.  Below I describe a possible solution.  I'm not 100% sure this is the best approach but it seems to work well and I'm currently working on a proof of concept app that will be thrown out in several weeks so there's little risk if I'm wrong.

Rails default formatting looks like this...


Bootstrap's CSS expects this....


The solution I've arrived at is to first remove the "field_with_errors" div.  This is done by setting the field_error_proc to simply return the html passed in without wrapping it.


The next step was to create a FormBuilder subclass with the desired functionality.  The example below only demonstrates the 'text_field' method.  In practice I'll have to create a separate method for each Bootstrap form element I want to use in my application.


Then to use the custom form builder I'd do somthing like this....


I realize this wasn't exactly a detailed explanation of a solution but hopefully I managed to get the gist of the idea across.


Dec 23, 2011

Just Doing It!

Almost 18 months ago a had an an epiphany.  I'm not exactly sure what caused the thought to form but I know when it happened.  I'm certain I know when because I shared the idea in an email with a friend the moment the thought completed synthesizing it's self.

It's taken me a long time to put together a plan that I thought would let me execute on the idea sucessfully but I think I've finally gotten there.  Today, I'm shifting gears.  I'm no longer just thinking about the idea, I'm acting on it.

In fact I'm jumping in with both feet right now!

Dec 22, 2011

Simple EC2 Configuration

Over the last few years I've used different approaches to scripting the configuration of my EC2 servers. Recently I discarded a more sophisticated approach for a very simple system.  This is all pretty obvious and I'm sure you'll notice that I stole this directly from RVM but it's working out so well for me I feel compelled to share anyway.

I created a public repository of scripts on Github.  Then, like RVM, I take advantage of the fact that Github allows access to the raw content of the master branch through a fixed url.  This allows me to execute commands like the one below which will run my script for installing the Passenger gem and Apache module:

sudo su -c "bash < <(curl -s https://raw.github.com/mgreenly/aws/master/shared/passenger.sh)" -

Then I combine that with a strict set of conventions; for example I always use Ubuntu so all of my scripts assume they are being executed as the ubuntu user.  This can make some of the scripts a bit tougher to write, for example when you have to sudo su to become postgres to execute database commands, but it makes it easy to execute one script after another in a chain right from your initial login.

This approach may not compare to using tools like Chef or Puppet but I'm finding I really like the simplicity of it.

Dec 17, 2011

EC2 Friendly SSH Config

If you spend lots of time in EC2 or any other cloud service your going to collect tons of junk in your $HOME/.ssh/known_hosts file. If you're remapping domain names to cloud based servers and those IPs change SSH's default settings will prevent you from connecting until the conflict is fixed. Instead of constantly having to edit known_hosts a better approach is to have SSH ignore known_hosts while you work in the cloud.

Here's my $HOME/.ssh/config. In addition to ignoring known_hosts I also set the identiy key file and the default user.

Host *compute-1.amazonaws.com
User ubuntu
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
IdentityFile ~/.ssh/your-ec2-key
ServerAliveInterval 30
ServerAliveCountMax 120


With these settings I can copy the DNS name straight from the AWS console, type 'ssh ' at the command line, paste the DNS name and then connect; example:

ssh ec2-107-100-123-99.compute-1.amazonaws.com