June 24, 2008

SSH Tunnel setup

It seems every time I setup a ssh tunnel, I struggle with something, usually its the directory permissions on the server machine, so here are the settings:

chmod 700 .ssh
chmod 600 authorized_keys2

Thanks to this site for troubleshooting advice.

May 15, 2008

IIS 6 and windows 2003 anonymous access problem

If you find yourself having the problem where accessing an IIS site from another machine is prompting you for a user/password and you have tried everything here and here and here. Then you could have my problem, which is I changed the password on the IUSR_ account. In this case you need to go and reset the password on the user for windows to something you know, then you need to go each iis website and click properties, directory security, then click edit, where you have checked "enable anonymous access" you need to put your new password in here and click ok. Magically it all works now (only cost me 4 hours of my life)

April 25, 2008

Passing your own paramaters to will_paginate page links

Lets say you have a search box with 3 types of search based on a drop down. If you use will_paginate by default clicks on the page links will lose the perspective of which drop down was chosen for the search. To fix this, you can pass a params hash to will_paginate view helper to pass along this context. For example:

If you had a search box for people , you can pass along the param from the search type like this:

<%= will_paginate @people :params => { :search_type => params[:search_type] } %>

April 24, 2008

On TV today

Had a really cool experience today being on NBC News. It was really interesting to see all the goes in to producing a news cast. The folks at NBC 4 in DC are really nice folks, thanks for having me out! To see the clip of me talking about KeepUp go here

April 22, 2008

TextMate Key Equivalent Problem & Possible solution

I have been working with TextMate for over a year now and I thought it was time I got good at using it. I used to be a Jedi Master of Visual SlickEdit, but when I moved over to the mac & ruby/rails I switched to TextMate. For the past year I have used largely the basic features of the editor and not much of the ruby & rails bundle features.

So my path to improvement started by buying and downloading the Peepcode TextMate for Rails 2 by Dr Nic. After about 5 minutes of watching, I decided I would try the shortcuts as I go,but I had a problem, none of the "Key Equivalent" shortcuts he was using in the video were working.

So what do I do, I had followed all the instructions in the screencast and some other hints on the web, but no luck. TextMate wont help since its a bundle, I emailed Dr Nic, but didnt get back a reply, and there is no nice forum for the rails bundle to post a question. So I went back to my old way of using TextMate for a couple weeks.

This time when trying to figure it out, I did some more digging and I figured out that when I installed the Rails2 bundle, it didnt add source.ruby to the scope selector, but it did add lots of the other file types. So I tried some "Key Equivalents" in a erb file and they worked fine. I added "source.ruby" to one of the commands and it started working fine in my controller/model, etc. So the question is, is this a bug in in the installer, is it ok to add "source.ruby" to all the Rails bundle items? Is there a conflict with the Ruby bundle? Anyone have any ideas?

I also recommend you get the beautify ruby in textmate macro and install it.

April 11, 2008

linux what version is installed? rpm hint of the day

If you want to know exactly which version of something is installed, including the architecture, this will do it

rpm -q --queryformat="%{n}-%{v}-%{r}.%{arch}\n" ProgramName

so for example:

rpm -q --queryformat="%{n}-%{v}-%{r}.%{arch}\n" ImageMagick
ImageMagick-6.2.8.0-3.el5.4.x86_64


March 03, 2008

Wifi Interference Mystery Solved - Wireless Security Camera

I had been bugging my friend who owns a local Deli for years to add free wifi so his customers could come and do some work while eating or having their morning coffee. When he finally agreed, what I thought would be a 30 minute setup turned into a serious scooby-doo mystery.
I setup the router as you would normally, and when plugged in, everything worked as planned, but when I tried to connect wirelessly. I could see the acess point, but my computer just wouldnt connect. Then, I would see the access point come and go. I tried the usual changing of channels and surveying what other stuff was going on on channels 1-11, but nothing worked. We returned the router thinking it was a hardware problem, but on the 2nd router, the same problem happened.
This time I took the router home and it worked fine, there was something weird about being in the Deli that was causing the problems. We thought it might be his wireless phones, so we unplugged them all and took out the backup batteries, but no luck.
We later discovered that the router seemed to work fine during off hours (not 9-5 mon-sat), but at 9am mysteriously it would just crap out. I was doing some testing one morning when I saw the Jeweler next store come in, and just when they came in, the thing stopped working. So we asked, what did you turn on, they said nothing, just the lights! My brother who makes his living doing this sort of thing (building way complex wireless networks for police, fire etc) said he would bring his Spectrum Analyzer the next time he came up, and that with his directional antenna, maybe we could track it down. The first time he came, it was a Sunday and unfortunately the problem was gone, so he couldn't find anything, everything was working great.
The next time we got there on a Saturday morning at 8:45 am and everything was working great, we watched for the Jeweler and sure enough, about 10 minutes after they came in, the problem started happening. We knocked on their door to ask what they just turned on, and they said "Nothing" again, we asked if we could bring in our Spectrum Analyzer, and after some negotiations with the manager she let us in (good think the Deli Owner knows them well), after about 5 minutes of walking around, my brother points up the the top left corner of a side room and says, whats that? (pointing to a wireless security camera), thats the source of the interference. We asked the guy if he had turned it on, he said no, its always on. What we figured out was its motion activated, so it goes to sleep during off hours, but once a employee or customer walks by, it turns on and starts transmitting. It turns out the Jeweler, was taking all the wifi spectrum, he was running his router on Channel 1, one camera on 6, and another on 11. The bad thing about these wireless security cameras is they dont show up on your computer when you are looking at whats out there, so you think, hey channel 11 should be clear. Also 802.11 networks generally get along better because of the protocols, but this camera was just totally blowing out the spectrum.
So the end solution is to, help the guy wire this camera, or buy him a newer 5.8 ghz
camera that will not interfere with the Deli wifi network!

February 12, 2008

How to troubleshoot & debug facebooker, facebook and rails

There are 3 Key things I have found invaluable in tracking down issues with Facebooker, Facebook and Ruby on Rails.

1.) View Source - If you are in development mode on facebooker and do a view source of the html page rendered in your browser, it will show you the fbml that facebooker generated and sent to facebook. This is  very handy when you are trying to figure out fbml rendering problems. Just compare what facebooker sent with what facebook expected and find your issue.

2.) Logging Facebook API calls - The default for facebooker is not to log any information sent via the HTTP API calls to facebook. Below is a version with logging added that shows the request and responses to facebook. So if you are getting parsing errors or just hanging, try this first and see what is going on.

* Please check your version of facebooker and make similar changes if the source has changed, please dont just blindly copy & paste this code in:

(in Service class within the facebooker plugin change the post method to: )
     def post(params)
            logger.debug "About to post to :" + url.to_s
            logger.debug "with params:"
            params.each { |elem| logger.debug elem[0].to_s + " : " +
     elem[1].to_s}
            resp,data = Net::HTTP.post_form(url, params)
            logger.debug "Result: " + resp.to_s
            logger.debug "Response Data: " + data.to_s
            Parser.parse(params[:method],resp)
          end

3.) Firebug - If you don't use firebug yet, you need to, it will save you a ton of time. Key things to use it for are for debugging ajax calls and css issues with your Facebook application.

February 06, 2008

Ajax, Rails & Facebook example

This post shows how you can do Ajax within a Facebook  Canvas using Ruby on Rails. This builds on my other facebooker examples (Facebooker Quick Start and Creating Facebook forms with Facebooker) , but the examples here don't really make use of any Facebooker specific features because I wanted to show the basics of how things worked,  you can in several cases use Facebooker helpers instead of the manual way I have done things.

First some background, since you are in a fbml canvas within a Facebook applications (diagram), and all requests are proxied through facebook & all javascript is facebook-ized (turned into fbjs), there are some limits as to what you can do. I suggest before trying anything you read  FBJS and FBJS Local Proxy.

Example #1 - Update a DIV with raw text & pass a value to the server - you must update the CALLBACK_URL_HERE with your callback_url & this example assumes your controller is called face_controller.

---------------------------------------------------------------------------------------------
View:

<script>
function do_ajax(div,val)  {
    var ajax = new Ajax();
    ajax.responseType = Ajax.RAW;
    ajax.ondone = function(data) {
       var p = document.getElementById(div);
       p.setTextValue(data);

    }
    ajax.requireLogin = true;
    ajax.post([CALLBACK_URL_HERE]/face/example1?someval=' + val);
}
</script>

<a href="#" onclick="do_ajax('ajax1','1234'); return false;">Update a DIV</a><br />

<div>
<span id="ajax1"></span>
</div>
<BR/>

---------------------------------------------------------------------------------------------
Controller:

def example1
    someval = params[:someval]
    logger.debug "I got the update task checked!"
    logger.debug "someval: " + someval.to_s
    render :text => "this is some raw text", :layout => false
  end


Example #2 - Make a remote call when a checkbox is checked and unchecked and use fbml to update div. Notice the subtle changes, we are using response type of FBML and we are setting the div using setInnerFBML, on the server we are returning text in FBML markup.

---------------------------------------------------------------------------------------------

View:

<script>
function do_ajax(div,val)  {
    var ajax = new Ajax();
    ajax.responseType = Ajax.FBML;
    ajax.ondone = function(data) {
       var p = document.getElementById(div);
       p.setInnerFBML(data);
    }
    ajax.requireLogin = true;
    var cbox = document.getElementById('check_me_now');
    var isChecked = cbox.getChecked();
   ajax.post('http://[CALLBACK_URL_HERE]/face/example2?ischecked=' + isChecked);
}
</script>
<span id="ajax1"></span>

<BR/>
<INPUT TYPE=CHECKBOX NAME="complete" id="check_me_now" onClick="do_ajax('ajax1','1234');" > Take out the trash
<BR/>

---------------------------------------------------------------------------------------------

Controller:

def example2
    is_checked = params[:ischecked]
    if is_checked == "true"
      txt_to_render = "<fb:success message=\"Task has been marked complete!\" />"
    else
      txt_to_render = "<fb:success message=\"Task has been marked incomplete!\" />"
    end   
    render :text => txt_to_render, :layout => false
  end

Example #3 - Make Ajax calls using local proxy and JSON protocol. In order to do this, your customers must have pretty recent versions of browsers with the flash plugin installed & you must have servers listening on port 80 (even to do dev or test). Fro JSON notice how we use data.content to access the content tag in the JSON.

---------------------------------------------------------------------------------------------
crossdomain.xml (must be in your rails public directory)

<?xml version="1.0"?> 
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <allow-access-from domain="apps.facebook.com" />
   <allow-access-from domain="apps.*.facebook.com" />
</cross-domain-policy>

---------------------------------------------------------------------------------------------
View:

<script>
function do_ajax(div,val)  {
    var ajax = new Ajax();
    ajax.useLocalProxy = true
    ajax.responseType = Ajax.JSON;
    ajax.ondone = function(data) {
       var p = document.getElementById(div);
       p.setInnerXHTML(data.content);
    }
    ajax.requireLogin = true;
    ajax.post('http://[CALLBACK_URL_HERE]/face/example3);
}
</script>

<a href="#" onclick="do_ajax('ajax1','1234'); return false;">Update a DIV</a><br />

<div>
<span id="ajax1"></span>
</div>
<BR/>
<fb:local-proxy/>

Controller:
def example3
    logger.debug "I got to example3!"
    render :text => "{\"content\":\"<p>From Server</p>\"}", :layout => false
  end

*Note, on this example I didn't see any parameters coming in from setting requireLogin = true, I have opened a question on this in the forum and will update when I know more.

Troubleshooting:

  1. I get the error in the browser: "XML Parsing Error: syntax error Location: http://apps.facebook.com/gottakeepup/test Line Number 1, Column 1:From Server "

    If your using setInnerXHTML to put the response, make sure the response is valid XHTML, if you are getting from server, change it to <p> from server </p>
  2. I get a bunch of text that starts with <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    You are probably using the canvas url instead of the callback url
  3. I see the call to to my server, but my UI is not getting updated properly

           Install firebug and debug the fbjs (its the javascript generated with the numeric prefixes)






February 05, 2008

Creating Facebook Forms with Facebooker

So now that you have your Facebooker Facebook application up and running you probably want to do something with it, right? The first thing I wanted to do was create a form that had some of my stuff in it and some Facebook stuff in it. So lets start with that.

One easy way to create a Facebook form that has the Facebook look and feel is using an fb:editor, an fb:editor form has 2 columns, one for the label and one for the UI component. By default the UI component will auto fill the entire area of the main column of the table:

Picture_2 As you can see on the image on the right this fb:editor is arranged into 2 columns. The left colum has labels like "About", "Title" , "Due Date" etc. The right column has the respective UI components.

The title component is a standard text box and
you can see how it expands to fill the entire area.

If you want to have several components in the area, you can put them inside a div and set the inline css of the div to manage the placement of these components (use relative placement only).

In our example form we want to have at least one of these label components and one date, and one Facebook UI component. I have chosen the friend finder as the example Facebook component.

Now that you have the background on fb:editor, lets get started with a simple version of the view to the right, in this case we will dumb the code down to 1 of each type of component that needs to be explained. This form shows an example create task form operating on the model Task. First the view code (new_task.rhtml) :

<BR/>
<% facebook_form_for(:task,@task,:url => url_for(:action => :create_task), :labelwidth => '75', :width => '500') do |f| %>

    <%=f.text_field :ttype, :label=> "Title"%>

    <fb:editor-custom label="Due Date">
      <%=datetime_select :task, :duedate,:order => [:month,:day,:year],:twelve_hour => true,:minute_step => 15, :use_short_month => true  %>
    </fb:editor-custom>

    <%=f.text(text_field_tag(:newrnumdays, 10), :label =>  "# Days")%>

    <fb:editor-custom label="Assign To">
        <%=fb_friend_selector()%>
    </fb:editor-custom> 

   <%=f.buttons "Add" %>
<% end %>

Now lets explain each piece of the form:

1)  <% facebook_form_for(:task,@task,:url => url_for(:action => :create_task), :labelwidth => '75', :width => '500') do |f| %>

Here we are defining the form it will call create_task method when submitted, note we are overriding the label and main column widths and assigning the form variable to "f".

2)  <%=f.text_field :ttype, :label=> "Title"%>

This is a regular old component, by using the :label attribute facebooker is smart enough to create the label and UI component for the fb:editor

3) <fb:editor-custom label="Due Date">
      <%=datetime_select :task, :duedate,:order => [:month,:day,:year],:twelve_hour => true,:minute_step => 15, :use_short_month => true  %>
    </fb:editor-custom>

This example is another way of doing the same thing, we can define the fb:editor tags ourself and simply place the label and the component in the proper spot. Why am I doing this instead of example #2 as its more complicated? At the time of this writing there is a bug in facebooker that makes it barf if you try to define a complex UI component in an fb:editor form (date, checkbox etc). So you need to do it the way I do it in #3 for now. Good to know how this works anyway.

4)    <%=f.text(text_field_tag(:newrnumdays, 10), :label =>  "# Days")%>

This example shows how you can use f.text to wrap a tag and have it be formatted as a fb:editor friendly field. You could do a tag as you did  in #3, but this is a bit nicer to look at.

5)    <fb:editor-custom label="Assign To">
        <%=fb_friend_selector()%>
    </fb:editor-custom> 

Now we want a Facebook component in our form right, this will define a fb_friend_selector field in your form.

6)    <%=f.buttons "Add" %>

Last but not least, we need a submit button for our form,  here is where we put it (dont forget the end to end off the form.)

Thats it, now you have a form that has the look of an fb:editor, and some of your components and a facebook component.

Note that in your controller, the only thing you will have to do differnt is fish out the facebook params as they are not part of your model, from the freind selector you can get:

  friend_name = params[:friend_selector_name]
  friend_id = params[:friend_selector_id]

From the parameters passed to you from facebook.

Good luck with your forms!