Archive for November 2005
I got a bad case of code formatting envy when I saw Cody’s post on .rjs templates. I’ve been happily running a somewhat dated Typo install since it basically worked, but when I read about the new (to me anyway) Macros, I knew it was time to make the leap.
In the past I’ve downloaded the source to my local machine, done whatever config there is, then uploaded the final pieces to my account. This time I thought I’d try the direct route. I made a backup of my typo directory and database, then did a svn checkout directly to my web account:
svn checkout svn://leetsoft.com/typo/trunk typo
I cd’d into the new typo directory and ran
rake migrate
But got these nasty errors.
So I panicked for a minute. DreamHost has upgraded to Rails 0.14.3, which is great, but I needed something newer, fresher, edgier. So I moved to my typo/vendor directory and
svn co http://dev.rubyonrails.org/svn/rails/trunk rails
And that was it! I ran migrate again and it worked like a charm. Now I have Trunk Typo on Edge Rails and it’s working great.
No tags
This is cool. Just when you think those Rails guys are about out of tricks, they pull this out of their hat.
If you’ve used the groovy AJAX tools in Rails much, you’ve probably hit the point where you have to update two elements on a screen. It gets ugly pretty quick, with calls to “evaluateremoteresponse” and “updateelementfunction” (as best I recall) and it really gets to be unclear what is doing what to whom and when.
Here’s a little addition/clarification to the demo that Cody posted.
In a controller, add these:
def fox
@header = "A Nice List of Animals"
end
def add
@new_item = params[:arg]
@header = "All Your List Are Belong To Us!"
end
def remove
end
Your fox.rhtml would look something like:
<h1 id='header'><%= @header %></h1>
<ul id='list'>
<li id='dog'>Dog</li>
<li id='cat'>Cat</li>
<li id='mouse'>Mouse</li>
</ul>
<div><%= link_to_remote("Add a Fox!",
:url =>{ :action => :add, :arg => "Fox" }) %></div>
<div><%= link_to_remote("Nuke the Cat!",
:url =>{ :action => :remove }) %></div>
Your add.rjs goes in your views directory next to the other views for that controller:
page.insert_html :bottom, 'list', content_tag("li", @new_item)
page.visual_effect :highlight, 'list', :duration => 3
page.replace_html 'header', @header
And also toss in a remove.rjs for fun:
page.visual_effect :shrink, 'cat', :duration => 1
And Voila! This is still a simple example, but you can see that it will sure help clean up Ajax-driven pages a whole lot.
No tags
If you’re having trouble connecting this snippet may help. This is handy as it runs in isolation from the rest of your Rails application which can be very useful in debugging.
Change the name of the class to be one of your table names (singular).
require ‘rubygems’
require ‘active_record’
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:host => “db.xyz.com”,
:username => “user”,
:password => “pass”,
:database => “dbname”)
class Person < ActiveRecord::Base
def initialize
puts “initialize”
super
end
end
t = Person.new
If you connect you won’t see any errors. Otherwise you should get an error that has some useful info.
No tags
I spent an evening trying to figure out why my RAILS_ROOT environment variable was out of whack the other night. It turns out that it is set relative to where you launch WEBrick from.
Usually I launch WEBrick from the root of my application:
>ruby script/server
For some reason, the other night I thought it would be handy to open my command window right in the script directory. I could then just use:
>ruby server
But then I got errors which turned out to be caused by RAILS_ROOT being set wrong which throws everything out the window.
I did get the chance to use the console to debug though, which just reinforced how cool that guy is. Just fire up:
>ruby script/console
And at the prompt, type:
RAILS_ROOT
And low and behold, the value is there to see.
It turned out I figured out the issue when I fired up WEBrick the next night from my usual location and everything just worked again.
No tags
