2013年9月23日月曜日

Simple introduction about what is Ruby on Rails



I begin to learn ruby on rails since begin of this August. I found out that ruby on rails is awesome, it speed up all my works and make it simple to maintain. So, I wrote this article to introduce about how awesome rails is.
Back to our topic, what is ruby on rails? Web application development framework with ruby language. For more details, you can do some searching on what is ruby on rails, there are many sources you can found on internet.

Installation

It is always easier to be understood by running up the things.
To install rails on your windows or mac PC. There is a quick way to install it by download rails installer and install it.
To install rails on linux os such as ubuntu, debian might needs a little bit more works then windows and mac. There are sources on internet, check it out.

To begin a new project

Before getting start to develop a new web application. You need to setup database, directory of your new project, .htaccess to control the traffic. Maybe you will spent almost half an hours to one hours to setup all those things. But …
Rails, will do it automatically for you.
By typing a few commands for it, rails will do it for you. Here’s the commands.
rails new MyProject
cd MyProject
rails generate model Item name:string price:integer description:text
rails generate controller items index new create show edit update destroy
rake db:migrate
rails server

On first command, create new project and name it "NewProject". Second command create table name item and columns of name, price and description. Third command create directory for html file, ruby file and routes. Fourth command to startup database and last command to startup the server.

Everything will be setup within 1 minutes. It means, you can begin your programming after 1 minutes. Cool right!

Go to http://localhost:3000 and you will see the welcome message of rails.

 rails2

To check with the project generated, go to http://localhost:3000/items

The page is blank. This is because all you have generated is database, system and routes. There will nothing to show unless html code is written.

Scaffold

A powerful tool that prepared by rails called Scaffold. Scaffold will generate everything including database, simple system code, traffic, html layout code. It means, you can begin to coding from half way instead of blank file.

How to use it? By a few simple command

First create a new project. In this case, named NewProject
rails new NewProject

Then, generate scaffold with following command and startup server.
rails generate scaffold item name:string price:integer description:text
rake db:migrate
rails server

Here’s the result generated by scaffold. Check it out

 rails1

Instead of blank project, scaffold will generate a template for us to make a quick way to start.

End

It is the end of the article, do you think that ruby on rails is awesome?

2013年9月14日土曜日

Event prevent default is not working


If you are working on key action with jquery and having problems on event.preventDefault is not working. Let’s me explain something simple to you.

event.preventDefault

As given example on docs, it shows us how it works on click action. For instances, prevent default action of anchor request the given url when clicked, action of submit button clicked to submit the form.
So far so good.

bind.( “keydown” , function( event ){ return false; } );

preventDefault is good but when it comes to key action such as enter key on submit form and tab key on switching the focus of input element. It lose it effects. You will found out that enter key are still performing default action and others key as well.
So… what is the solution? Adding return false on handler. Snippets below shows an simple example on this solution.
$(document).bind('keydown', function(e){  
  
 return false; 
 
}); 

It’s works right!

What happens if you want only ‘enter’ key to be prevent default action. Here’s the solution.
$(document).bind('keydown', function(e){  
 var keyPressed = ('which' in e) ? e.which : e.keyCode;
  
 if( keyPressed == 13){         
  return false;
 }else{
  return true;
 }
 
}); 

Enter keycode is 13. Return true on event handler will allow the key to continue with default action and return false will stop event handler from continue to default action.



It’s still not working


There are a few quite common mistake make by people, especially for me. Either you too?


  • Errors inside the handler function

If there is errors (exp. syntax errors) occured inside the handler function, default action will be carry out even though there is return false in handler function. Don’t tried to ignore the errors that shows on your browser, it might be the core of your problems.


  • Mess up with JQuery instances and native Javascript instances

What does it means? Check it out with the differences at following codes.
$("ul").children("li")[0].html("");
$("ul").children("li")[0].innerHTML = "";

It is quite tricky problems, html() is jquery properties and innerHTML is javascript properties. But on this case, children(“li”) returns native javascript element array, so there is no method for html(“”) in returned element array. So, second code is the correct answer.

If you have do some checking by Object.keys( $(“ul”).children(“li”)[0] ); you will found out that the properties is different from jquery properties.

 


Conclusions


High level language doesn’t means you don’t have to understand the basic of it. Understanding basic sometimes will be the helper on solving problems.

Good luck

2013年9月6日金曜日

How to open select input method option menu programmatically on Android

Here's the snippets for Android to open the select input method option menu programmatically.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (mgr != null) {
    mgr.showInputMethodPicker();
}