Archive for May, 2010

How to set up a postgresql user for rails (on Ubuntu)

Sunday, May 30th, 2010

This took a little while to figure out, and existing documentation wasn’t super helpful for me, so here are some notes.

First off, you create new postgres users with the createuser command, but every time I ran it I was getting an error like this:

createuser: could not connect to database postgres: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

What got me over that hump was installing postgresql-8.3 instead of 8.4, which is the default you get on Ubuntu when you install the postgres package. This is what I wanted anyway, since Heroku (my host) uses 8.3:

sudo /etc/init.d/postgresql-8.4 stop
sudo apt-get remove postgresql
sudo apt-get install postgresql-8.3

If you want to check if your postgresql server is running, run ps ax | grep post. You should get something like this:

 4417 ?        S      0:00 /usr/lib/postgresql/8.3/bin/postgres -D /var/lib/postgresql/8.3/main -c config_file=/etc/postgresql/8.3/main/postgresql.conf
 4420 ?        Ss     0:00 postgres: writer process                                                                                                    
 4421 ?        Ss     0:00 postgres: wal writer process                                                                                                
 4422 ?        Ss     0:00 postgres: autovacuum launcher process                                                                                       
 4423 ?        Ss     0:00 postgres: stats collector process                                                                                           
 4482 pts/4    D+     0:00 grep post

Now you can create your user:

sudo su postgres
createuser -A -d --pwprompt the_username

And then you should be able to create your databases with rake db:create.

If you are getting errors like this:

rake db:create
(in /path/to/your/app)
FATAL:  Ident authentication failed for user "your_username"
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:968:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
...

… it is because Postgres expects there will be a UNIX user that corresponds to each postgres user. When it talks about “Ident” it’s talking about the UNIX authentication system. You could just create a UNIX user with the same credentials as your postgres user, but I took a different route.

As suggested by Felix Sun, I edited my pg_hba.conf (which on Ubuntu is in /etc/postgresql/8.3/main). I didn’t do exactly what he did though, I just added another line as follows:

local   all         postgres                          ident sameuser
local   all         all                                  md5

As far as I understand, that just means that any user connecting locally on any database is OK as long as they authenticate with a password.

I restarted postgres (/etc/init.d/postgresql-8.3 restart) and ran my rake db:create; rake db:migrate and I was in business.

My startup

Saturday, May 29th, 2010

I don’t know how many people subscribe to this blog, since it’s basically just a dump of random snippets of code. But if you do and you’re curious what I’ve been referring whenever I’ve mentioned “my site” over the last year, I just launched it a couple of weeks ago, so you all can see it.

It’s called SproutRobot, and it tells you when to plant your vegetable garden and sends you seeds! It’s still quite a mess, but it does something useful, and it will hopefully only get better. At some point you just have to take the plunge.

Look forward to future posts about optimizing activerecord-driven sites for speed, I’m sure. :)

Rails forms for has_many relations

Saturday, May 29th, 2010

This is pretty cool. Say you have a model with a has_many relation, and you want to edit the model and all it’s children on one page. Rails can take care of all of this, automatically, with the standard controller. You just need this in your parent model:

class User < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts
end

And you need a form like this:

<% form_for(@user) do |f| %>
  <%= f.label_for :blog_name %>
  <%= f.text_field :blog_name %>
 
  <% f.fields_for :posts do |p| %>
    <%= p.label_for :title %>
    <%= p.text_field :title %>
    ...
  <% end %>
 
  <%= f.submit %>
<% end %>

And that’s it! You can use all the craziness that the form helpers do (grouping radio buttons, converting to values booleans, etc) and it all works out.

It’s exciting. I feel like I’m finally getting into the really good stuff with Rails.

Setting the default background color for transparent PNGs

Friday, May 7th, 2010

I’ve been using .png images on my site, which are great for putting on top of gradient backgrounds. But unfortunately IE6 just displays a flat background for them.

Probably the best thing to do would be to switch to transparent .gif images with carefully chosen background colors that will blend reasonably on the anticipated background color. But that’s a lot of work.

Instead, I’m just setting the background color in the .pngs to something close to the gradient background. You can still see the box, but whatever. If you’re using IE6 you obviously don’t care if the web sites you visit look funky.

The way to do this is just to create an extra layer in The GIMP below your other layers, fill it with the background color you want in IE6, and then set the transparency to 0%. Won’t affect modern browsers.

IE6 text invisible until selected and deselected

Friday, May 7th, 2010

I’m embarking on the painful journey of making my site passable usable in Internet Explorer. Not fun.

First bizarre bug: sometimes text is simple not displayed, until you select it and then deselect it. And then it’s there. Nice work, Microsoft.

I was able to fix it by changing the nesting of my divs and form elements.  I changed this:

<div>
  <form>
  ...
  </form>
</div>

… to this:

<form>
  <div>
  ...
  </div>
</form>

Lots to look forward to.

Failed search terms: ie6 text hidden/invisible unless I select it, deselect