Oct 21 2008

adding blackbird javascript logging to your rails project

Blackbird is a nifty JavaScript logger that can be added to any web app. Ever find yourself adding alerts into your JavaScript to get some (foggy) idea of what’s going on? Blackbird makes this kind of debugging much easier.

So how do you get Blackbird working in your Rails app? By following these easy steps:

1. Download the Blackbird zip file.
2. Extract contents of zip file.
3. Copy blackbird.css into public/stylesheets
4. Copy blackbird.js into public/javascripts
5. Copy blackbird_panel.png and blackbird_icon.png to public/images
6. Edit blackbird.css, modify urls to blackbird_panel.png and blackbird_icon.png to contain “/images/” in their path; “url(blackbird_panel.png)” should become “url(/images/blackbird_panel.png)”
7. Include the blackbird Javascript and CSS files in your layout: <%= stylesheet_link_tag 'blackbird' %> and <%= javascript_include_tag 'blackbird' %>
8. Add a button to open the Blackbird console: <input type=”button” onclick=”javascript:log.toggle();” value=”show log”/>
9. ???
10. Profit!
11. Check out the Blackbird website to learn more about using Blackbird.


Sep 14 2007

rails: building urls to static html in public dir

In rails when you create links in your view it’s best to use link_to or url_for tags when creating urls. However, both of these methods take as arguments the controller and/or action to generate a link for. If you’re trying to link to something in the public directory, like a static html file, then there is no controller or action. Instead of hardcoding the link, use compute_public_path:

<%=compute_public_path('file-name-without-extention','parent-dir','extension')%>

So if you’re trying to link to a file public/static_files/foo.html just do:

<%=compute_public_path('foo','static_files','html')%>


Aug 17 2007

getting thickbox, jquery, and prototype to play together nicely

The latest version of the JumpBox admin interface uses ThickBox for its cool visual effects, and prototype for handling AJAX requests. The problem is that ThickBox depends on jQuery, and jQuery and prototype don’t like working together.

Both jQuery and prototype define “$” as an alias in the global namespace. When you try to use the “$” shortcut and both libraries are included, the browser complains. Lucky jQuery gives you a way to override “$” and let prototype use it thus resolving the conflict:

 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     jQuery.noConflict();
   </script>
 </head>
 <body>
 </html>

After calling jQuery.noConflict(), you can still use jQuery “$” functionality, but you have to call it using “jQuery(’foo’)” instead of “$(’foo’)”.

Now that jQuery and prototype can coexists peacefully, how does ThickBox work? Well… out of the box it doesn’t. ThickBox uses the “$” syntax, but expects to use jQuery’s implementation of it. So to get ThickBox to work, you need to replace all instances of “$” with “jQuery” in thickbox.js. Now ThickBox should be using jQuery exclusively, jQuery and prototype won’t conflict over “$”, and everything should work fine and dandy. Note: I’ve only tried this using ThickBox 2.1.1, jQuery 1.1.1, and prototype 1.5.0, but it should work on the latest and greatest versions.


Jun 8 2007

rails: multiple request values with the same name

Let’s say you have a web app that takes input from the user. How about a phone book app for storing phone numbers. And let’s assume that each person in your phone book can have an unlimited number of phone numbers. In the interface you allow the user to fill in as many phone numbers as they’d like by dynamically creating more and more input=’text’ elements. When the user saves, the data that gets posted to the server looks something like:
/phonebook/save?number=5203842233&number=6029130923&number=9283125248
Basically there are multiple parameters that have the same name with different values. Now in Java to get the values of the numbers you would do something like…
request.getParameterList('number');
…to get an Array of Strings, each element containing one of the numbers (NOTE: my Java is rusty and this could be wrong). In rails there is no such getParameterList method. As I currently understand it, rails only sees the first number. So it thinks that number=5203842233 was the only value passed to the server, the other values are ignored. Looking at rail’s logs confirms this:
Parameters: {"controller"=>"phonebook", "action"=>"save", "number"=>"5203842233"}
So how do you get multiple values with the same name out of the request? You add ‘[]‘ to the parameter name (ie <input type=”text” name=”number[]“/>):
/phonebook/save?number[]=5203842233&number[]=6029130923&number[]=9283125248
Now looking at the log file:
Parameters: {"controller"=>"phonebook", "action"=>"save", "number"=>["5203842233","6029130923","9283125248"]}
When retrieving params[:number], you’ll end up with an Array of the numbers instead of just the first.


Jun 7 2007

rails: getting ip address from request

This is useful if you want to log the ip address of the person using your rails app (except when the person is behind a proxy, thanks James!):
request.env['REMOTE_ADDR']
Taken from this ruby thread.


Oct 26 2006

desert code camp

The second Desert Code Camp is coming up this weekend.

“Code Camp is a free, one-day event put on by the local Phoenix community to help promote software development in general. There is no right or wrong language, platform, or technology. If a topic relates in any way to the code that causes a machine to produce a desired result, it’s welcome here.”

The previous code camp was ok, but the sessions were too jammed together. There was no time to meet people and just talk. Everyone kept running from session to session trying to keep up. Some of the sessions were good, while others were a waste of my time.

I’ll be attending again, hopefully they’ve tweaked the schedule enough so it’s no so jam-packed. There aren’t any sessions that are must-attends for me, so I’ll mostly be there to hang out and support the community. If nothing else, at least it’s a free lunch.

Edit: Apparently lunch isn’t free.


Jun 28 2006

missing gems/plugins

The designer of a project I’m working on decided to use the RedCloth ruby gem in the project we’re working on. He installed the gem on his local machine and added “require ‘RedCloth’” to environment.rb. Now, since he installed a gem and ruby gems by default are stored in the ruby installation directory, the source repository did not contain the RedCloth library.

Unforunately I forgot to get the RedCloth gem. So when I updated my project source from SVN the project broke output in development.log. I eventually figured out that I was missing the gem. So I downloaded the gem, then moved the whole RedCloth folder into the vendor/plugins directory of the rails project.

The lack of error messages pointing me to the “require ‘RedCloth’” was annoying to say the least. Maybe the require statement should be moved someplace else? Someplace after the logger has been initialized maybe? I wonder if other error/issues in enviroment.rb will make WEBrick die out too. Maybe you can start WEBrick with some advanced debugging for these kinds of situations? Hmm, I guess I should do a little research…