Saturday, November 28, 2009

Rails, JSON, & JQuery, Oh My!

So far I've solved 2 of 3 problems (I'm using Rails 2.3.3):

I may not have the error messages verbatim, comment and correct me where that's the case

405 Method not allowed


If you're sending JSON your request.raw_post (in params_parser.rb) ought to look like JSON, in which case you can't use _method => 'PUT' or _method => 'DELETE'. Since you can't rely on the x-www-form-urlencoded values, you have to use a header instead.

// Set the request for any of $.ajax, $.post, $.get, $.getJSON
var method = 'PUT' // or 'DELETE'
$.ajaxSetup({ 
        beforeSend: function(xhr) {xhr.setRequestHeader("X-HTTP-Method-Override", method);},
        scriptCharset: "utf-8",
        contentType: "application/json; charset=utf-8"
      });
$.ajax({
      url: "/questions.json",
      type: "POST",
      data: $.toJSON( {
          question : {
              text : "Life, Universe, & Everything?",
              answer : 42,
              tag : "42"
          }
      },
      dataType: "json",
      success: function(msg){
         alert(msg);
      }
   }
);

INSERT INTO ModelX('val1', 'val2', 'val3') VALUES(NULL,NULL,NULL)


Although the JSON is parsed into Ruby hashes and arrays, it isn't parsed into your model. In the Controller you have to add something like this
if params[:_json]
    params[:question] = params[:_json][:question]
end

:_json is the converted request.raw_post (your POST body).
:question is the name of my model, yours will be different, of course.

Allow method override for GET

Still working on it....

Wednesday, November 25, 2009

[OT] A Public Google WAVE

Normally I post here on how to fix things, but this post is just how to do something.

There are at least three simple ways to make a wave public.
  1. Public contact
    • In the Contacts Pane click the + next to Manage Contacts
    • type in public@a.gwave.com
    • ignore that it is not a contact
    • ignore the greyed out submit button
    • hit enter
    • Rejoice the Public is now a contact
    • select a wave
    • add Public to the wave

  2. EasyPublic Bot
    • select a wave
    • add easypublic@appspot.com
    • Rejoice that easypublic added public
    • remove easypublic

  3. add Public from a public Wave
    • search 'with:public'
    • select a wave
    • click the globe icon
    • select add to contacts

If you have Google WAVE you should see a wave here, otherwise you should see the video for it.

Tuesday, November 17, 2009

client / server GIT

ssh myuser@www.coolaj86.info # remote server
cd ~/Code
mkdir chess.git
cd chess.git
git --bare init
exit

git clone myuser@www.coolaj86.info:~/Code/chess.git
# This does the same as the above
#mkdir chess
#cd chess
#git init
#git remote add origin myuser@www.coolaj86.info:~/Code/chess.git

# change some files
git add .
git commit -m "Changed some files"
git push origin master

For my next project for my programming class I've decided to use git on my server as well as the 'ol faithful lappy... just in case lappy dies or something.

Many thanks to the great blogger Tim: http://toolmantim.com/articles/setting_up_a_new_remote_git_repository

Reverting to an earlier version in GIT

What good is a version control system if when you make that critical mistake you can't find the 'undo' button? Not much.

So if I want to see a list of past commits with comment, date, and files changed and revert to a specific version of a file:

git log --pretty=medium --abbrev-commit --name-status
commit 66408e0
Author: AJ ONeal 
Date:   Fri Nov 13 01:22:16 2009 -0700

BST memory leak fixed

M       Makefile
M       src/WebCrawler.cpp
M       tpl/BST.h
M       tpl/VectorSet.h

git checkout 66408e0 tpl/VectorSet.h

This came about because I was working on a homework project and I had made my VectorSet unnecessarily complicated and introduced some bugs. I wanted to revert to a previous (and simpler) version of the file... but I hadn't learned how to do such a thing yet.

After poking about in the man page I realized that the intuitive 'git revert' wasn't what I wanted and I thought 'git rev-list' was... but I was wrong. Thankfully, the fellows at my local LUG pointed me in the right direction.





Thursday, November 12, 2009

C++ Template with a Private Class: "expected constructor, destructor, or type conversion before '*'"

Within a class or function template, it is necessary label (with the
"typename" keyword) any name-dependent type - bytes.com

typename BST<TypeT>::BSTNode * BST<TypeT>::BSTNode::Insert(TypeT const& v)
{
//Implementation
}

I was creating a BST and LinkedList with private node classes and I ran into this error:
"expected constructor, destructor, or type conversion before '*'"

There are 3 root causes:
* referencing "BST" instead of "BST<TypeT>"
* referencing "BST<TypeT>::BSTNode *" as a return value instead of "typename BST<TypeT>::BSTNode *"
* attempting to use the standard ".h" / ".cpp" separation in the first place rather than just garbling everything together

Sunday, November 1, 2009

Fast File Copy in Linux (Ubuntu)

After a hiatus from doing proper backups (though I have done some improper backups) I finally got that HFS+ disk copied over to two other partitions, reformatted the disk as ext4, and started copying the data back.

Since much of the data was secondary backup I used simple rsync quite a bit on the first copy over
#copying HFS+ volume to ext3 volume
rsync -avh /mnt/tmp /mnt/backup


rsync would have been way too slow to do the exact copy once I reformatted the drive so I used tar instead. With my local SATA / SATA connection I seem to be getting several gigs per minute this way. Another nice touch to this approach is that it preserves hard links.
tar c /mnt/backup/ | tar xf - -C /mnt/tmp


If I were doing the same thing over a network, it would have been like this:
localhost-src:
tar cz /mnt/backup | nc remote-dest-ip-or-name 2342
remote-dest:
nc -l -p 2342 | tar xzf - -C /mnt/remote-backup