| Subcribe via RSS

Rails Tutorial : Add testing data by using migration

December 18th, 2009 | No Comments | Posted in Rails, Ruby

From previous tutorial Rails Tutorial : Beer shop Part I – Scaffolding I have show how to create a very simple scaffolding. This tutorial I will show how to create a testing data. Nobody wants to type SQL statement or add item manually every time they want to use the data. Rails has provided a handy tool to support this already.

1. add testing data

2. This will create a file in migrate folder ready to use

1
2
3
4
5
6
7
class AddTestData < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end

3. We now add test data by editing file add_test_data.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class AddTestData < ActiveRecord::Migration
  def self.up
    Beer.delete_all
    Beer.create(:name => "Artvelde",
      :abv => "5.4%",
      :bottle_size => 250,
      :case_size => 24,
      :price => 30.03)

    Beer.create(:name => "Biere Du Boucanier Dark Ale",
      :abv => "9%",
      :bottle_size => 330,
      :case_size => 24,
      :price => 55.64)

    Beer.create(:name => "Bush Ambree",
      :abv => "12%",
      :bottle_size => 250,
      :case_size => 24,
      :price => 50.98)

    Beer.create(:name => "Cantillon Grand Cru Bruocsella",
      :abv => "5%",
      :bottle_size => 750,
      :case_size => 6,
      :price => 46.5)
  end

  def self.down
    Beer.delete_all
  end
end

Rails Tutorial : Beer shop Part I – Scaffolding

December 18th, 2009 | 1 Comment | Posted in Rails, Ruby

I have decided to jump to rails, because I have seen how fast to build an application and how easy it is. I have been a Java Programmer quite a long time, and played with Grails and Rails as well. So, I decided to create this tutorial for my benefit to practice my rails techniques. It is an very simple beer shop. I will start from scratch.

Prerequisite.
1. Netbeans (I use Netbeans because it provides the rails plugin and everything with one click.
2. MySQL
That’s it. Let’s start

1. Create a rails application
create rails application

2. Call it BeerShop
name

3. Configure database: you have to put your own attributes; username and password that correspond with your database configure, don’t worry you can change it later at file database.yml
configure database

4. Click Finish

So now, you should have your first rails application created. Try and run it you will get this page. If you don’t get this page, you might have to check with your database configuration. If you are developing non-database application you might have to turn off the database connection.
first rails page

Next, we are creating a scaffolding, this magic command will create everything for us, CRUD and a simple page
5. Right click on your project and choose generate
generate

6.
Model Name:

1
beer

Attributes Pairs (field:type) :

1
name:string abv:string bottle_size:decimal case_size:decimal price:decimal

Oh, ahh and don’t for get to create a database in MySQL as well

1
CREATE DATABASE BeerShop_development

create beer

Now we have got a schema already, we just have to migrate those to our database, by using db:migrate command or run

1
rake db:migrate

db:migrate

You will now have a beer already. try to run http://localhost:3000/beer on your browser. We will have CRUD beer ready to use. It’s a very ugly page. We will jazz with it later.

beer

I’m gonna continue this next week.

Tutorial – My Personal Dictionary by Google Translate

December 17th, 2009 | No Comments | Posted in Javascript

As Google provides us the AJAX support for Google translate and other very useful API to access the most powerful API from Google. I tried to play a little bit with the Google Translate to build my very simple dictionary Eng – Thai.

Here is the code, which is very simple and straightforward.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Dictionary Eng - Thai</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    function callBackTranslate()
    {
      var word = document.getElementById("word").value;
      google.language.translate(word, "en", "th", function(result) {
        if (!result.error) {
          var container = document.getElementById("translation");
          container.innerHTML = result.translation;
        }
      });
    }
   
    function translate()
    {
        google.load("language", "1", {"callback" : callBackTranslate});
    }

    </script>
  </head>
  <body>
    <div>
        <input type="text" id="word" name="word"/>
    </div>
    <div>
        <input type="button" value="Translate" onclick="translate();"/>
    </div>
    <div id="translation"> </div>

  </body>
</html>

50 Beautiful Free Icon Sets For Your Next Design

December 15th, 2009 | No Comments | Posted in Knowledges

http://www.smashingmagazine.com/2009/12/14/50-beautiful-free-icon-sets-for-your-next-design/

Tutorial : How to use javascript to force users only put Number on textfield

December 1st, 2009 | No Comments | Posted in Javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function numberOnly ( e )
    {
        var keynum;
        var keychar;
        var numcheck;

        if(window.event) // IE
        {
            keynum = e.keyCode;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which;
        }
       
        keychar = String.fromCharCode(keynum);
        numcheck = /\d|[\b]/;
       
        return numcheck.test(keychar);
    }