18 Dec 2008 @ 9:49 AM 

JSON is really simple for those that haven’t had a chance to use it yet, maybe it’s just me but when I stumble across a new TLA or FLLA (that’s a three letter acronym or a four letters long acronym) I usually very apprehesive. But I can give you an all-you-need-to-know JSON tutorial right here.

JSON is a light, text-based XML substitute.
A JSON string is just a list of keys & values : { ‘key’ : value, ‘key’: value, ‘key’: value, … }
The value part can be either:
A string  { ‘forename’ : ‘mark’ }
An array { ’skillz’ : [ 'ruby', 'rails', 'jquery', 'linux'] }  (yeah, I’ll crack the jokes)
A hash { ‘likes’ : { ‘food’ : ‘lasagna’, ‘movie’ : ‘the thing’, ‘team’ : ‘celtic’ } }
You can also nest these so that array can contain hashes and hashes can contain arrays.

So far so good yeah? Well that’s it, that’s all you need to know about JSON.

But here is the cool bit, if you have the JSON stored in a string called json_string you can do this : var json_object = eval( ‘(’ + json_string + ‘)’ ) and then access all the values in the new json_object programmically!

Lets look at a simple example

function json_test()
{
var json_string = "{ 'astring' : 'simple string', 'ahash' : { 'key1':'value1','key2':'value2'},'anarray':['zero','one','two'] }";
var json_object = eval( '(' + json_string + ')' );
alert(json_object.astring); // outputs 'simple string'
alert(json_object.ahash.key1); //outputs 'value1'
alert(json_object.ahash['key1']); // alternative way to access a hash
alert(json_object.ahash.key2); // outputs 'value2'
alert(json_object.anarray[0]); // outputs 'zero'
alert(json_object.anarray[1]); // outputs 'one'
alert(json_object.anarray[2]); // outputs 'two'
}

This is a massively handy way of passing information to your javascript in an easy to edit form that is programmically accessible, XML originally promised the same thing but has bloated out over the years to the point where xml parsers are fairly hefty bits of code and XML has lost some of it’s human-readability. JSON gives us a quick ‘n’ dirty way to pass information between browser & server in a lightweight way.

So there is my 5 minute tutorial, it probably only took you 3 minutes so have a cup of coffee on me.

I’ll put a post up soon showing a more in-depth use of JSON and how I used it to give a web app I’m working on an extendible, flexible dynamic way to create complex forms on the fly.

Tags Tags: ,
Categories: Jquery
Posted By: admin
Last Edit: 21 Dec 2008 @ 10 30 AM

E-mailPermalinkComments (0)
 13 Dec 2008 @ 7:11 PM 

I’m managed to totally fuck up moving my home/ directory onto the bigger SSD on my Eee PC. I tried to rollback my changes by copying the folder back to the correct place (as root) and removing stuff I’d added to fstab.

And then rebooted my machine.

Bad news, by copying the folder as root I’d effectively locked myself out my machine as Ubuntu doesn’t like it when the user folder isn’t writeable by the user. The error message that popped up suggested I fix the problem in a failsafe session. This was news to me but after a quick and panicked google I found that you can drop into a terminal window by pressing Alt+Ctrl+F1.

Handy to know

Tags Tags:
Categories: Eee-PC, Linux
Posted By: admin
Last Edit: 15 Dec 2008 @ 11 18 PM

E-mailPermalinkComments (0)
 12 Dec 2008 @ 4:28 PM 

Was thinking about this the other day, so I thought I’d state it out-loud so to speak.

While I think this theme total rawks this is not a tactict acceptance of a certain OS that might bear a passing resemblance to it.

Vista still sucks ass. (’Mon the penguins)

Tags Tags: , ,
Categories: General
Posted By: admin
Last Edit: 12 Dec 2008 @ 04 28 PM

E-mailPermalinkComments (0)

As soon as I’d made up my mind to get an Eee PC 901 I had it in the back of my mind to get this done. However my first couple of attempts ran aground upon the pointy rocks of my general linux ignorance. But, to quote Homer Simpson: ‘If at first you don’t succeed, give up’, so I did.

Today however through sheer presistence and force of will I managed to get BackTrack 3 installed on a shiny new 4gb SD card with Eee Pc friendly graphics mode and persistent changes saved also.

I did a combination of these two guides

Install BT3 on USB with Persistent Changes

Install BT3 on USB with Eee PC Graphics Mode

If you want to set it up on your Eee PC 901, I’ll point out the differences I found with the Persistent Change guide.

The first difference is how the poster Umattu refers to his SD location (sdb, sdb1, sdb2), the Eee PC 901 comes with 2 internal drives and sdb refers to the second 12Gb one (on Linux). When you put a SD card in the slot it is identified as ’sdc’ so make sure you change any references in sdb to sdc when you are running through the guide.

I notice Umattu talks about getting the BT3 USB edition as a rar file, but I downloaded the USB in iso format (from here) so I opened it Archive Manager and copied the 2 directories over from there (Boot and BT3).

Follow the guide down to the section that begins

Using your favorite text editor we need to modify the syslinux.cfg file. Here I will use nano
Code:
nano syslinux.cfg

Here we take a little side track to add the Eee PC 901 graphics mode stuff.

Download the lzm file from here

Copy it into the /BT3/Optional folder

Now edit syslinux.cfg (I’m using gedit)

sudo gedit syslinux.cfg

And add this section at the top after the PROMPT, TIMEOUT, DEFAULT lines

Label EEE

MENU LABEL EEE Mode with Persistence

KERNEL /boot/vmlinuz

APPEND vga=785 initrd=/boot/initrd.gz ramdisk_size=6666 root=/dev/ram0 rw changes=/dev/sda2 load=901_net_gfx

Now go into the second partition on the sd card and create a directory called ‘changes’.

Once you’ve done that you can pick up Umattu’s guide at the section beginning

Run the bootinst.sh script:

Code:
./bootinst.sh

Eh, voila – you should be rocking BT3 happily on your 901 with persistent changes.

N.B. Minor thing but I noticed it didn’t save the settings when I changed the keyboard to GB, but when I went into the config panel for the keyboard and removed all the other countries then applied the changes it saved them okay.

Tags Tags: , , ,
Categories: Eee-PC
Posted By: admin
Last Edit: 12 Dec 2008 @ 04 15 PM

E-mailPermalinkComments (9)
 11 Dec 2008 @ 10:16 PM 

UPDATE: Hands up I made a boo-boo, I had listed the variable that holds the token as ‘form_authentication_token’ and it should have been ‘form_authenticity_token’, many apologies for the inconvenience.

So I was trying my first ajax calls to a rails backend and I hit this ugly error -

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)

I hunted about on google till I found the answer. The problem is that Rails automagically adds a randomly generated token ( bunch of characters ) that it inserts into any forms created with any of the form helpers (form_for, form_tag etc).  When the form is posted Rails checks the token is valid before processing the request.

This code

<h1>New productsetupinfo</h1>
<% form_for(@productsetupinfo) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :setup %><br />
<%= f.text_area(:setup, :cols=>"80",:rows=>"30") %>
</p>
<p>
<%= f.label :note %><br />
<%= f.text_area(:note, :cols=> "64", :rows=>"1") %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', productsetupinfos_path %>

Produces this html

<form action="/productsetupinfos" class="new_productsetupinfo" id="new_productsetupinfo" method="post">
<div style="margin: 0pt; padding: 0pt;">
<<< Authenticity Token automagically added here >>>
<input name="authenticity_token" value="7054c89779d7c1b6ed64e5261b49de76d1539974" type="hidden">
</div>

<p>
<label for="productsetupinfo_setup">Setup</label><br>
<textarea cols="80" id="productsetupinfo_setup" name="productsetupinfo[setup]" rows="30"></textarea>
</p>
....<snip>

The problem with making an ajax call is that Rails doesn’t have the opperchancity to insert the authenticity token, so it’s up to you to do it manually. Fortunately you can access the authenticity token in your views using form_authenticity_token. You only need to make 2 changes to your code to get it working.

To test this we’ll create a page that doesn’t work and then fix it so it does.

First : Create a new Controller. From the root of your rails app

script/generate controller Authfix index js_responder

Next : Create methods in new controller

class  AuthfixController < ApplicationController

def index
end

def js_responder
respond_to do |wants|
wants.js { render :text => 'AJAX successful - Barman! Lard for everyone!' }
end
end

end

And finally create the view

<html>
<head>
<<< You may need to change this to point to your jquery library >>>
<%= javascript_include_tag "jquery">
<script type="text/javascript">
$(document).ready(function()
{
$('#makecall').click(function()
{
$.post('/authfix/js_responder',{ somedata:'somedata' },function(data,status){ $("#response").html(data) },'text');
}
});
</script>
</head>
<body>
<input type="button" id="makecall" value="Press me" /><br />
<div id="response">Nowt yet</div>
</body>
</html>

Now run this and click the button and nothing will happen, if you check your logs you will see our friend the InvalidAuthenticityToken error rearing its head.

Btw, interestingly if you make an Ajax call and pass no data in the 2nd $.post argument it works fine.

To fix the problem you need to store the auth token in a js variable (if available) and automatically add it to any ajax calls

class  AuthfixController < ApplicationController

def index
end

def js_responder
respond_to do |wants|
wants.js { render :text => 'AJAX successful - Barman! Lard for everyone!' }
end
end

end

And finally create the view

<html>
<head>
<<< You may need to change this to point to your jquery library >>>
<%= javascript_include_tag "jquery">
<script type="text/javascript">
$(document).ready(function()
{
<<< Store Authentication Token here >>>
var AUTH_TOKEN = "<%= protect_against_forgery? ? form_authenticity_token : '' %>";
<<< Automatically add token to ajax calls >>>
$.ajaxSetup({data:{authenticity_token : AUTH_TOKEN}});

$('#makecall').click(function()
{
$.post('/authfix/js_responder',{ somedata:'somedata' },function(data,status){ $("#response").html(data) },'text');
}
});
</script>
</head>
<body>
<input type="button" id="makecall" value="Press me" /><br />
<div id="response">Nowt yet</div>
</body>
</html>

Add the two lines above and re-run the app and you should now get a response after clicking the button.

Result.

Tags Tags:
Categories: Jquery, Rails
Posted By: admin
Last Edit: 11 Feb 2009 @ 11 30 AM

E-mailPermalinkComments (0)
 11 Dec 2008 @ 3:57 PM 

Handy little snippet for loading data into a Mysql database from a csv file.


*N.B.  if you are importing files in windows you will need to change that last line to – lines terminated by ‘\r\n’.

Tags Tags:
Categories: General
Posted By: admin
Last Edit: 26 Feb 2009 @ 05 28 PM

E-mailPermalinkComments (0)
 10 Dec 2008 @ 2:00 PM 

Howdy,

This blog is going to be a handy dumping group for all the web-dev stuff I learn, leaning heavily on Ruby, Rails and Jquery – a web development triumvirate that I’m a big fan of.  I’ve got a big project at work that is using all three and I’ll be learning as I go along but I’m very much in Get-It-Done mode at the moment which means that I’m pretty much hacking out a working system just now without worrying about ( or being aware of ) half the nice stuff that Ruby/Rails/Jquery provides.

I’m noticing that while I’m knocking together this app that there are a lot of areas of Rails in particular that I’m currently glossing over and I’m not too keen to complicate the job by adding in new automagical functionality without a clear idea of what it does or how it works. My plan is to explore these areas and write up small test projects that clearly show the how, why, where and what’s of a certain section that I’m unfamiliar with.

Hopefully this will build out to be a useful resource for people like myself, starting out in R/R/J and looking for some examples as well as being a handy brain dump for myself.

If anyone has any requests, questions, (most likely) have spotted some gross errors in my code or simply want to rant and shout then fire away.

Cheers

Tags Tags:
Categories: Eee-PC, General, Jquery, Linux, Rails, Ruby, Uncategorized
Posted By: admin
Last Edit: 07 Jul 2009 @ 02 32 PM

E-mailPermalinkComments (0)
\/ More Options ...
Change Theme...
  • Role »
  • Posts »
  • Comments »
Change Theme...
  • VoidVoid (Default)
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LiteLightweight