Archive for August, 2007
Rails and JavaScript: page.call gotchas
Posted by Giovanni Intini | Filed under Ajax, Javascript, Programming, Prototype.js, Rails, Ruby, Ruby on Rails
Ruby on Rails has wonderful out of the box javascript support, but sometimes implementing dynamic user interfaces is not so easy as it seems.
In an application I’m working on I have a list of people with a checkbox each. In the load event of the page I add a click handler to every checkbox using this javascript code:
$$('.ConfermaInvitati').each(function(element) { Event.observe(element, 'click', clickHandler); }); // ... function clickHandler(event) { var e = Event.element(event); new Ajax.Updater(e.up(), 'my/invited/toggle', { parameters: { id: e.up().up().id }, onLoading: function() { e.src = "/images/admin/spinner.gif"; }, onSuccess: function() { new Ajax.Request('my/refresh', {}) } }); }
This works fine until I add a new person via an Ajax call. That person won’t have a clickHandler because the element wasn’t on the page when I called the click handler. So I thought it was time to test page.call in the render :update block I had in the rails application.
I tried this code:
render :update do |page| # Do stuff that creates the new objects and adds it to the page # The data I need is in @invited page.call("Event.observe($$('##{@invited.permalink} .ConfermaInvitati').first()), 'click', clickHandler)")
Obviously that didn’t work, and it turned out I have to read documentation before doing fancy things
The rails docs told me that I had to use call passing the function name as the first argument and an array of parameters as the second argument, the problem is that call turns all the parameters into strings—this means I could not pass the clickHandler function to Event.observe.
I found the solution in <<. If you do page << "foo", foo will be evaluated as raw javascripts. This meant I was able to do
page << "Event.observe($$('##{@invited.permalink} .ConfermaInvitati').first()), 'click', clickHandler)
and finally have the functionality I was looking for. So remember, don’t page.call if you need to pass javascript variables to your functions.
links for 2007-08-30
Posted by Giovanni Intini | Filed under Del.icio.us
-
Really interesting.I’ll keep an eye on it.
links for 2007-08-29
Posted by Giovanni Intini | Filed under Del.icio.us
-
Prototype.js based newsticker. Nice design.
Sudoless Rails Stack on OSX
Posted by Giovanni Intini | Filed under Apple, Mac, Productivity, Programming, Rails, Ruby, Ruby on Rails
More and more of my developer friends are switching to OSX as time goes by, and they keep asking me for directions on the best setup for rails development: how to install ruby, how to install the missing libraries, and so on.
My professional development life started on a PB Titanium running OSX 10.2, and continued through a PB Aluminium and a Macbook Pro. Each time I changed laptop I also reinstalled everything again, and each time I tried to come up with a better setup.
Now I’ve finally found a setup I’m comfortable with, and it has the following advantages:
- Sudoless: everything runs from my user directory
- Non-system-tampering: doesn’t touch files in the original osx installation
- Crash-proof-easy-reinstall: you can just delete everything and reinstall without fear of rendering your system unstable
- Fink based: uses everything it can use from the fink repositories
Now that I’ve sold you on my setup
it’s time to explain how to implement it. Step one: XCode and Developer Tools
If you don’t have XCode already installed you can install it from the disks you got with your Mac or, better, download the newest version from Apple Developer Connection. Once you got it installed you can proceed to the next step.
Step two: Fink
Go to the Fink Download Page, and get the package that works best on your Mac (intel or powerpc). Follow the installation instructions and install the base fink system. After you’ve done that add these lines to your /.profile
# Dev Enviroment LDFLAGS=-L/sw/lib export LDFLAGS CPPFLAGS='-I/Users/your_username/unix/include -I/sw/include' export CPPFLAGS
_/Users/your_username/unix/include_ doesn’t exist yet, but we’ll create it when installing ruby.
Step three: Ruby from Sources
This is the first tricky part. Before OSX 10.4.6 the Ruby version shipped by Apple didn’t work with rails, so you were on your own. Now it works, but I prefer to have ruby in my home directory so I can mess with the sources and with the gem files and upgrade painlessly. So download the latest ruby sources and unpack them wherever you wish (I like /src).
Now you’re ready to go. First make sure you have readline and readline5-shlibs installed via fink so you can have a comfortable irb environment:
user$ fink install readline readline5-shlibs
After that it’s time for ruby:
user$ cd <sub>/src/ruby-1.8.6 user$ ./configure <del>-prefix=/Users/your_username/unix user$ make && make install
The whole magic (and it’s not a big magic btw) is in -prefix. Installing ruby will create the /Users/your_username/unix path. Now it’ time to add the unix dir to $PATH. Edit /profile once again and add these lines:
PATH=/Users/your_username/unix/bin:/usr/local/mysql/bin:/usr/local/sbin:$PATH export PATH
Step four: MySQL
Quite simple with the packages from mysql.com.
Step five: Last but not least, rubygems
Fetch the gem package from RubyForge and install it:
user$ cd src/unpacked-rubygems-directory user$ ruby setup.rb
Step six: This is the real last step
– Rails and a little hackNow you’re free to install rails (and friends) using
user$ gem install rails --include-dependencies user$ gem install mysql-ruby user$ gem install capistrano --include-dependencies user$ gem install mongrel --include-dependencies
The small hack I was talking about is a symlink:
user$ sudo mv /usr/bin/ruby /usr/bin/ruby-apple user$ sudo ln -s /Users/your_username/unix/bin/ruby /usr/bin/ruby
This way applications that insist on using /usr/bin/ruby (TextMate’s RubyMate for example) will work fine.
Have fun with your self-made rails stack
links for 2007-08-27
Posted by Giovanni Intini | Filed under Del.icio.us
-
Fantastic image resizing algorithm.
links for 2007-08-24
Posted by Giovanni Intini | Filed under Del.icio.us
-
Some tips on upgrading to Cap2, from Topfunky.
Capistrano 2 Callbacks
Posted by Giovanni Intini | Filed under Capistrano, Programming, Rails, Ruby, Ruby on Rails
Converting the callbacks in my cap recipes to the new capistrano 2 format wasn’t as easy as I thought it would be. It turned out I had to use fully qualified task names in the after callback instead of the non-namespaced-names.
Here’s a sample of working callbacks:
namespace :deploy do after "deploy:setup", "deploy:sposivip:create_galleries", "deploy:sposivip:freeze_rails" after "deploy:update", "deploy:site5:link_public_html", "deploy:sposivip:link_rails", "deploy:sposivip:link_galleries" end
Here I have two callbacks. The first one runs after setup, creating shared paths and freezing rails in the shared directory, so I avoid having a copy on rails in each release.
The second callback runs after deploy:update, so it will be called whenever I do a simple deploy or a cold deploy, it links back various directories in the shared path and it links the shared rails in vendor.
links for 2007-08-10
Posted by Giovanni Intini | Filed under Del.icio.us
-
Gotta love the Japanese.
links for 2007-08-09
Posted by Giovanni Intini | Filed under Del.icio.us
-
Technology meets magic.
links for 2007-08-08
Posted by Giovanni Intini | Filed under Del.icio.us
-
Note to self: try this out.
-