Menu

Thursday, December 16, 2010

The command used to create RoR project with Mysql

I have many versions of Rails in my computer and I want to create a RoR Project bases on Rails 3.0.3, so I must use the following commands to generate my project.
rails _3.0.3_ new rails303 --database=mysql

YOUR_PATH\rails303>bundle install


After you config username and password (that'll be used to connect to mysql database adapter) in database.yml file (YOUR_PROJECT_PATH/config/database.yml)

Type "rake db:create" in the terminal to create the database (its name is the same as you config in database.yml)

To specify the version of rails

You can specify the version of rails when you create an application by typing the following when creating a new app.
 rails _3.0.1_ new app_name 

Wednesday, December 15, 2010

Interactive Ruby (irb)

If you type "irb" in the terminal, you'll get the following screen ...


You can type a ruby code on the terminal, for example, it'll return 14 if you type 7+7.


You can also create a function. For example, function hi will print "Hello World!!!" on the screen.


Moreover, you can enter any ruby commands on the terminal.
Finally, irb'll help you to test any ruby commands without wasting your time because irb runs the ruby command immediately.

Error while installing mysql in command line (behind a proxy)

You'll encounter this error while enter "gem install mysql" command if your proxy requires authentication (you behind a proxy), so you need to include your proxy server into the command "gem install --http-proxy http://USERNAME:PASS@HOST:PORT mysql".

OR For installing another gem : "gem install --http-proxy http://USERNAME:PASS@HOST:PORT gem_name"

Bundle install

This is my solution. It may has a better solution than mine.

For Mac user like me, if you create the RoR project with NetBeans you need to copy the following files:

  1. config.ru
  2. Gemfile
  3. Gemfile.lock
to your project folder. These file was created from Terminal(I use this one). Otherwise, you cannot type "bundle install". The program will show "Could not locate Gemfile", if you don't do above.

Tuesday, December 14, 2010

To use another ROR project in netbeans

Note: you have to config via cmd in case that netbeans cannot generate "rake db:create or migrate" command.
1. type "bundle install"
2. open cmd in the ROR project directory then type "rake db:create" 
3. type "rake db:migrate" then run rails server via command line
4. after that you can run it as normal on your own computer

How to install RoR on Windows

1. download "rubyinstaller-1.9.2-p0" from the ROR website and set it path by click on the check box

2. install rails by  command "gem install rails"

note: you can check version by put -v after command

3. update gem by "gem update --system"

4. copy libmysql.dll from C:\Program Files\MySQL\MySQL Server 5.1\bin OR C:\Program Files\MySQL\MySQL Workbench 5.2 CE into E:\Ruby192\bin OR C:\Program Files\MySQL\MySQL Server 5.5\lib

5. install mysql by "gem install mysql"

6.download "rubygem"

7. If you encounter the above error when you enter "rake db:create" command , delete "rake.gemspec" in C:/Ruby192/lib/ruby/1.9.1/gems/specifications

Monday, December 13, 2010

Adding field(s) in a table

Shortcut to generate migrations that add fields to a table:
rails generate migration add_fieldname_to_tablename fieldname:type

Reminder: Database.yml

In the case that your password in database.yml is a pure number such as 123.

You'll get this alert "TypeError : can't convert Fixnum into String"

Therefore, you have to cover the number by using "" for example "123".

Tuesday, November 30, 2010

'jQuery' is undefined

Don't forget to import jquery library first!

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

After that, you may import another jquery files (If you have).

Tuesday, November 23, 2010

Don't forget to delete "rake.gemspec"

C:/Ruby192/lib/ruby/1.9.1/rubygems.rb:340:in `bin_path': can't find executable r
ake for rake-0.8.7 (Gem::Exception)
from C:/Ruby192/bin/rake:19:in `<main>'

If you encounter the above error when you enter "rake db:create" command , delete "rake.gemspec" in C:/Ruby192/lib/ruby/1.9.1/gems/specifications or C:\Ruby192\lib\ruby\gems\1.9.1\specifications

Tuesday, November 16, 2010

MySQL Commands (1)

- Create a database: mysql> create database [databasename];
- List all databases: mysql> show databases;
- Use another database: mysql> use [db name];
- Show all tables in DB: mysql> show tables;
- Show field formats: mysql> describe [table name];
- Delete a database: mysql> drop database [database name];
- Delete a table: mysql> drop table [table name];
- Dump all databases for backup: -u root -p --opt >/tmp/alldatabases.sql
mysqldump -u username -ppassword –all-databases > dump.sql
- Dump one database for backup:
-u username -p --databases databasename >/tmp/databasename.sql
ysqldump -u username -ppassword database_name > dump.sql
Export A MySQL Database Structures Only
mysqldump -u username -ppassword –no-data database_name > dump.sql
Backup Only Data of a MySQL Database
mysqldump -u username -ppassword –no-create-info database_name >dump.sql
- Dump a table from a database:
-c -u username -p databasename tablename > /tmp/databasename.tablename.sql
- Restore database (or database table) from backup:
-u username -p databasename < /tmp/databasename.sql

How to import .sql into DB in RoR

In database.yml:


development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: DBName_development

  pool: 5
  username: root
  password: Your Password (If you have)
  host: localhost


test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: DBName_test
  pool: 5
  username: root
  password:  Your Password (If you have)
  host: localhost


production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: DBName_production
  pool: 5
  username: root
  password: Your Password (If you have)
  host: localhost



After editing the database.yml file, go to the root of the Rails application via command-line


--->  rake db:create

This command will create the database or DBName_development in MySQL.



The following command can be used to your import SQL file (.sql) into DBName_development:



mysql -u root -p app_development < path_to_.sql/filename.sql
If SQL file is in another directory, for example, you can use ~/Desktop/filename.sql

A complete migration can be applied to the development database from the command line (inside your application's RAILS_ROOT directory):  rake db:migrate

Change root password in MySQL

- When you want to access mysql via command-line, you must use ---> mysql -uroot -p instead of ---> mysql -u root -p

- To change root password --->
$ mysql -uroot -p
mysql> use mysql;
mysql> update user set password=PASSWORD("NEWPASSWORD") where User='USER';
To reload privileges:

mysql> flush privileges;
mysql> quit

Monday, November 15, 2010

RoR

- To create RoR project:  rails new {directory name}

- Before running server, you should make sure that you are inside the directory of your rails application.

- To start the server: rails server

- To stop the server: ctrl+c

- To delete all files that rails generated for you:

  • rd /S /Q {directory name}   <<--- For Windows
  • rm -rf {directory name}  <<--- UNIX
- Copy libmysql.dll into E:\Ruby192\bin (C:\Program Files\MySQL\MySQL Workbench 5.2 CE)

To bind RoR and MySQL together: gem install mysql

- To delete RubyGems libraries (previously versions): gem cleanup

- To create a database with MySQL: rails new {directory name} --database=mysql   <<--- Don't swap the location of each word


- Don't forget "rails new" command when you create a new rails application!!!

Wednesday, November 3, 2010

Fix web page's size

Fix web page's size
Put in the <head> to fix web page's size

<script language="javascript">
window.onload=function(){
 window.moveTo(0,0);
 window.resizeTo(screen.availWidth,screen.availHeight);
}
</script>

Monday, November 1, 2010

Curve Corner

If you want to make curve corner, add these lines into your css file:

/* Begin: Making curve corners */
 border: 0px solid #cce5fc;
 border-radius: 10px;   /*IE9*/
 -moz-border-radius: 10px;   /*Firefox*/
 -webkit-border-radius: 10px;   /*Safari/Chrome */
 -o-border-radius: 10px;   /*Opera*/
 /* End: Making curve corners */

After you add these lines, you'll see curve corner if you access the web page via IE9, Firefox, Safari, Chrome, and Opera browsers.


*For IE browser, these lines of code support IE9 only.
The following picture is the result when you access the web page using IE8.

Thursday, October 14, 2010

Introduction to Rails

Rails is an open source (no license fee) web application framework for the Ruby programming language that will help you build powerful web sites quickly, with code that’s clean and easy to maintain. Rails was developed by David Heinemeier Hansson in the early 2000s and was released to the public in July 2004. It's also released under the MIT license. Rails is based on the MVC or Model-View-Controller architecture.