Friday, April 11, 2014

How to write a static site in haml and quickly serve it on localhost (OSX)

While reading a blog post about responsive design I decided that I would learn better if I actually coded along with the author and saw the effects in my own browser. So I wanted to write some html and css, and view it in my local browser. For reasons too mysterious (or mundane) too list, my goals were:
  • I wanted to make a directory,
  • type an html file into it
  • access that file by using localhost:1234 like I do rails and sinatra (not File -> Open in my browser - ugh!)
  • actually type haml because I can't stand carets
The steps are very simple:
  • The haml gem has a command line tool. You say "haml infile.haml outfile.html" and it works
  • A tool called watch turns a single-run command into an ongoing loop
  • There's a homebrew formula for watch, so you can "brew install watch"
  • After it's installed, you can run "watch haml infile.haml outfile.html"
  • Stack Overflow, quoting Aaron Patterson finishes the cake by giving you a one-liner to serve your current directory using ruby: "ruby -run -e httpd . -p 9090"

Wednesday, September 14, 2011

rails server: Address already in use

You might get this error if you killed a rails server with ctrl-z

To fix:

in a terminal type command
lsof | grep ruby

The output will look like this:


From here you can see the PID of the process that's tying up your port. In the gist above it's 1754. Kill it with

kill -9 1754

Thursday, August 11, 2011

"undefined method `text?' for nil:NilClass" when using :uniqueness validator

Short answer: if your validation line says this ...

... then change it to reference column name instead of association name like this ...


... and you're all set! Have a good day.




For an explanation of why this happens, read these steps I used to re-create ("..." denotes output I didn't paste in here):

terminal:


tagging.rb:


terminal:


console:


I threw a debugger call into active_record's uniqueness.rb file:


and dove back into the console


now line 57 is making sense


back to tagging.rb:


... and it works!

Monday, August 8, 2011

pg gem - ERROR: Failed to build gem native extension - OSX 10.6

I got the following error while installing the pg gem on OSX 10.6:


Installing pg (0.10.1) with native extensions /Users/luke/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/installer.rb:483:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

/Users/luke/.rvm/rubies/ruby-1.8.7-p302/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/luke/.rvm/rubies/ruby-1.8.7-p302/bin/ruby
--with-pg
--without-pg
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config


Gem files will remain installed in /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/pg-0.10.1 for inspection.
Results logged to /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/pg-0.10.1/ext/gem_make.out
from /Users/luke/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/installer.rb:446:in `each'
from /Users/luke/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/installer.rb:446:in `build_extensions'
from /Users/luke/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/installer.rb:198:in `install'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/source.rb:96:in `install'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/installer.rb:55:in `run'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/spec_set.rb:12:in `each'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/spec_set.rb:12:in `each'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/installer.rb:44:in `run'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/installer.rb:8:in `install'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/cli.rb:226:in `install'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/vendor/thor/task.rb:22:in `send'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/vendor/thor/task.rb:22:in `run'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/vendor/thor/invocation.rb:118:in `invoke_task'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/vendor/thor.rb:246:in `dispatch'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/lib/bundler/vendor/thor/base.rb:389:in `start'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/gems/bundler-1.0.10/bin/bundle:13
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/bin/bundle:19:in `load'
from /Users/luke/.rvm/gems/ruby-1.8.7-p302/bin/bundle:19


The solution was to add pg_config to my path. (Note it existed because I had already installed PostgreSQL). To locate the pg_config executable I ran:

~ $ mdfind pg_config | grep bin | uniq
/Library/PostgreSQL/9.0/bin/pg_config


Then I added "/Library/PostgreSQL/9.0/bin" to my path, and ran


~ $ gem install pg -v=0.10.1
Building native extensions. This could take a while...
Successfully installed pg-0.10.1
1 gem installed


success!

Thanks to for pointing that solution out to me.


Saturday, September 25, 2010

for coders: How to respond to a random error message

Situation: your program has just failed and some cryptic error message is staring you in the face. You have no idea what the error message means or how to resolve it, or even where to start looking. You're stuck.

what follows is a basic recipe that has freed me from thousands of such snags:

1. open a browser that supports tabs: basically anything these days

2. in the first tab, go to google search, paste in error message, hit enter. Can also try putting quotes around search, which makes google match the whole thing word-for-word, instead of separate search terms.

3. for each of the links that catches your attention, open that link in a new tab. Make sure you leave the original "search results" tab unchanged - it should still be showing the search results.*

Now you're opening various links from those search results and you've got two or three tabs open.

4. skim each of the pages and close un-promising ones. What you're looking for is a discussion thread where someone else has encountered the problem, and/or documentation for the module that lobbed the error. Should take you not too long to determine whether each of those pages holds the answer, or part of it.

5. soon, enough information and problem is solved. Even if new error message, at least you got past one

voila, un-stuck!

6. (optional) bookmark these pages. At this point there are one, two, or maybe three web pages open in tabs. As collected unit, contain the information turned out solved the problem. This is a valuable product of your work; should save it. For saving: some browsers allow you to bookmark collection of tabs. Can try naming the bookmark with the original error message in case it comes up again, if you like.


notes:
* Keeping the search results available in a window means you don't have to re-perform the search, which will happen when none of the first handful yielded anything useful. With the search results right there, you can work by closing un-useful pages and opening new ones as you browse through the internet's holdings.


Saturday, June 19, 2010

Deploying to Heroku Bamboo Stack: missing gem missing newline

So this took up a lot of my time and so this may save you some:

If you are getting weird behavior - check your .gems and environment.rb files and make sure they end in a newline character.

Done.

Sunday, November 22, 2009

habtm polymorphic, in Rails 1.2

Here's a setup that worked for me, using rails 1.2

class Job < ActiveRecord::Base

has_many :truck_assignments, :as => :truck_owner
has_many :trucks, :through => :truck_assignments

end


class TruckAssignment < ActiveRecord::Base

belongs_to :truck_owner, :polymorphic => true
belongs_to :truck

end


class Truck < ActiveRecord::Base

has_many :truck_assignments
has_many :truck_owners, :through => :truck_assignments

end


This allows me to do:
script/console
j = Job.find(:first)
j.trucks
# => [array of Truck objects]

However, I cannot do: sometruck.truck_owners

about this blog:

I am a self-taught Ruby on Rails programmer, so I depend on the community-of-strangers that is the Internet for so many little bits of information. This blog is here for one purpose: to save you five minutes at a time by giving you a solution or answering a question that took me more than five minutes to figure out.

The format is intended to be incredibly simple:
Given a certain context A
When I run into problem B
Then I get past it by doing C

The name? That refers to the ten postulates that form the foundation of Euclid's geometry. Basically I just like the sound of the name.

My other websites:

Search This Blog

Loading...

Followers