Saturday, April 30, 2011

No such file or directory - /tmp/mysql.sock

Fix : sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

Reference : No such file or directory

How to customize Flash messages in Devise Rails gem

Customize the config/locales/devise.en.yml file in your project.

undefined local variable or method `confirmed_at' for

Devise Rails plugin throws this error when you don't have the

t.confirmable

in the users migration.

/etc/hosts file messed up on Snow Leopard

Whenever I run a Rails app, I had to access it via http://0.0.0:port_number. Devise Rails plugin sample app barks when redirecting with this error message:

ERROR URI::InvalidURIError: the scheme http does not accept registry part: 0.0.0:3000 (or bad hostname?)


1) To cleanup the mess, I edited the /etc/hosts file so it now looks like this:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost

2) Then flush the cache to pickup the new changes by running :

sudo dscacheutil -flushcache

Now you can access your local Rails apps by : http://localhost:port_number and Devise is happy again.

Thursday, April 28, 2011

sudo: no tty present and no askpass program specified

When trying to deploy to a new staging server, I get the following
error:

sudo: no tty present and no askpass program specified

Solution:

Add the line: default_run_options[:pty] = true
in your deploy.rb

Tuesday, April 19, 2011

how to get all the branches that is remote in git

If you have many remote branches that you want to fetch at once:

$ git remote update
$ git pull --all

Now you can checkout any branch as you need to, without hitting the remote repo.


http://stackoverflow.com/questions/67699/how-do-i-clone-all-remote-branches-with-git

Sunday, April 17, 2011

Source code formatter for Blogspot

Very useful source code formatter for blogspot

Ruby Programming Language Book Notes 2

| operator is used for union operation in Array.
& operator is used for intersection operation in Array.


1: class Array
2: def union(another)
3: self | another
4: end
5: def intersection(another)
6: self & another
7: end
8: end

Saturday, April 16, 2011

Ruby Programming Language Book Notes

1)

minimum = if x < y then x else y end

can also be written as :

minimum = x < y ? x : y

2)

class Fixnum
def inclusive(element)
self..element
end

def exclusive(element)
self...element
end
end

This eliminates the mental mapping from .. and ... to the behaviour of the methods.

3) This does not work:

class Fixnum
alias inclusive ..
alias exclusive ...
end

gives syntax error, unexpected tDOT2

So how do you redefine the .. and ...? If you do ri Fixnum, it shows the instance methods. The section on Operators has discussion on operators that can be re-defined.

Thursday, April 14, 2011

Convert Haml to ERB

1) script/plugin install http://github.com/cgoddard/haml2erb.git

2) In console:
hamls = Dir["app/views/**/*.haml"] - ['app/views/layouts/screen.html.haml'];
hamls.each do |haml|
puts haml
erb = haml.sub(/\.haml$/, '.erb')
File.open(erb, 'w') do |file|
file.write Haml2Erb.convert(File.read(haml))
end
end

http://makandra.com/notes/544-convert-haml-to-erb

ERROR: There has been an error fetching the ruby interpreter. Halting the installation.

This happens when there is no network connection. How wonderful it would be if developers can give error messages that make sense! Make the code robust and give users a clear call to action to rectify any errors that they can recover from. It is surprising that even ruby gems has the same issue.

Sunday, April 03, 2011

Mispelled HTTP_REFERER in Rails 3.0

The correct spelling is HTTP_REFERRER but Rails source has it with wrong spelling. So in order to get the HTTP_REFERRER do the following in your controller:

request.env["HTTP_REFERER"]

request.domain has been deprecated. The domain method has been moved to ActionDispatch::Http::URL. Ideally we want to use the method defined in this module.Do to metaprogramming it is difficult to figure out the actual keys that are used in ActionDispatch::Request. If you run this snippet:

request.env.keys.each do |x|
logger.info x
end

You will get all the keys:

action_dispatch.request.formats
action_dispatch.request.parameters
rack.session
HTTP_ACCEPT
HTTP_HOST
SERVER_NAME
rack.request.cookie_hash
action_dispatch.remote_ip
rack.url_scheme
HTTP_KEEP_ALIVE
HTTP_USER_AGENT
REQUEST_PATH
action_dispatch.request.query_parameters
action_dispatch.request.unsigned_session_cookie
SERVER_PROTOCOL
HTTP_ACCEPT_LANGUAGE
rack.errors
action_dispatch.secret_token
async.callback
REMOTE_ADDR
PATH_INFO
rack.run_once
rack.version
SERVER_SOFTWARE
action_dispatch.request.path_parameters
rack.request.cookie_string
SCRIPT_NAME
HTTP_REFERER
action_dispatch.parameter_filter
HTTP_COOKIE
HTTP_VERSION
rack.multithread
action_dispatch.request.request_parameters
action_dispatch.cookies
REQUEST_URI
rack.multiprocess
rack.request.query_hash
SERVER_PORT
HTTP_ACCEPT_CHARSET
action_controller.instance
rack.session.options
async.close
REQUEST_METHOD
warden
rack.request.query_string
action_dispatch.request.content_type
GATEWAY_INTERFACE
HTTP_CONNECTION
HTTP_ACCEPT_ENCODING
QUERY_STRING
rack.input

Now you can access any of these environment values from your controller similar to the referrer.

Saturday, April 02, 2011

Accessing Webrick running on VM from Mac OS

1) If the VM is running, stop it.

2) To forward from port 3001 on the host to port 3000 on the guest, run the following command on the host.
$ VBoxManage modifyvm "Ubuntu" --natpf1 "webrick,tcp,,3001,,3000"

3) To delete the port forwarding run:

VBoxManage modifyvm "Ubuntu" --natpf1 delete "webrick"

4) Now you can go to http://localhost:3001 on your host to hit webrick running on your VM.

The page loads very slow. Is there any way to speed it up?