render syntax
1.render action
render(:action=>"hello")
2.render text
render(:text=>"welcome to use ruby on rails language")
3.render action
render(:template=>"demo/index")
2010年11月18日 星期四
create one to many relationship
1.use has_one create connection with page on subject
#vim app/models/subject.rb
add new line in it
has_many :pages
2.use belongs_to create connection with subject on page
# vim app/models/page.rb
add two line in it
belongs_to :subject
has_many: sections
3.use belongs_to to create connection with page on section
add new line in it
belongs_to :page
3.use console create connection on subject with page
#rails console
subject = Subject.find(2)
page = Page.first
subject.pages << page
4.use size to get the account for subject
subject.pages.size
subject.pages.empty?
subject.pages.delete(page)
subject.pages[1]
#vim app/models/subject.rb
add new line in it
has_many :pages
2.use belongs_to create connection with subject on page
# vim app/models/page.rb
add two line in it
belongs_to :subject
has_many: sections
3.use belongs_to to create connection with page on section
add new line in it
belongs_to :page
3.use console create connection on subject with page
#rails console
subject = Subject.find(2)
page = Page.first
subject.pages << page
4.use size to get the account for subject
subject.pages.size
subject.pages.empty?
subject.pages.delete(page)
subject.pages[1]
create one to one relationship
1.use has_one create connection with page on subject
#vim app/models/subject.rb
add new line in it
has_one :page
2.use belongs_to create connection with subject on page
# vim app/models/page.rb
add new line in it
belongs_to :subject
3.use console create connection on subject with page
#rails console
first_page = Page.new(:name=>"first page",:permalink=>"first",:position=>1)
first_page.save
second_page = Page.new(:name=>"second page",:permalink=>"second",:position=>1)
second_page.save
4.use subject to get id 2 on Subject
subject = Subject.find_by_id(2)
subject.page = second_page
#vim app/models/subject.rb
add new line in it
has_one :page
2.use belongs_to create connection with subject on page
# vim app/models/page.rb
add new line in it
belongs_to :subject
3.use console create connection on subject with page
#rails console
first_page = Page.new(:name=>"first page",:permalink=>"first",:position=>1)
first_page.save
second_page = Page.new(:name=>"second page",:permalink=>"second",:position=>1)
second_page.save
4.use subject to get id 2 on Subject
subject = Subject.find_by_id(2)
subject.page = second_page
2010年11月17日 星期三
find the record
1.use rails console to enter the console
#rails console
2.find the record, find, find_by_id, find_by_name
#subject = Subject.find(2)
#subject = Subject.find_by_id(2)
#subject = Subject.find_by_name("first subject")
3.find all record
#Subject.all
4.find first or last record
#Subject.first
#Subject.last
#rails console
2.find the record, find, find_by_id, find_by_name
#subject = Subject.find(2)
#subject = Subject.find_by_id(2)
#subject = Subject.find_by_name("first subject")
3.find all record
#Subject.all
4.find first or last record
#Subject.first
#Subject.last
variable scope
local variable
$global variable
@instance variable
@@class variable
CONSTANT
:symbol
ClassName
$global variable
@instance variable
@@class variable
CONSTANT
:symbol
ClassName
2010年11月16日 星期二
find,update and delete one record
1.use rails console
#rails console
2.use subject.find
#subject=Subject.find(2)
3.update attribute
#subject.name="twenty man"
#subject.save
or you can use update_attribute
#subject.update_attributes(:name=>"powerman",:position=>2)
4.destory one object
#subject.delete
#subject.frozen?
#rails console
2.use subject.find
#subject=Subject.find(2)
3.update attribute
#subject.name="twenty man"
#subject.save
or you can use update_attribute
#subject.update_attributes(:name=>"powerman",:position=>2)
4.destory one object
#subject.delete
#subject.frozen?
create new record
1.use console
#rails console
2.create new subject instance
subject = new Subject
subject.new_record?
subject = Subject.new(:name=>"first man",:position=>2,:visible=>true)
subject.save
3.create sceond subject instance
subject = Subject.create(:name=>"second man",:position=>1,:visible=>true)
subject.save
4.use mysql command to confirm this
#mysql -u root -p
use cms_development
select * from Subjects;
#rails console
2.create new subject instance
subject = new Subject
subject.new_record?
subject = Subject.new(:name=>"first man",:position=>2,:visible=>true)
subject.save
3.create sceond subject instance
subject = Subject.create(:name=>"second man",:position=>1,:visible=>true)
subject.save
4.use mysql command to confirm this
#mysql -u root -p
use cms_development
select * from Subjects;
migrate syntax
1.create new migrate name AlterUsers
#rails generate migrate AlterUsers
2.modify db/migrate/20101117011002_alter_users.rb
modify file like this
class AlterUsers < ActiveRecord::Migration
def self.up
rename_table("users","admin_users")
add_column("admin_users","username",:string,:limit=>25)
change_column("admin_users","email",:string,:limit=>100)
rename_column("admin_users","password","hashed_password")
add_column("admin_users","salt",:string,:limit=>40)
puts "*** about change table"
add_index("admin_users","username")
end
def self.down
remove_index("admin_users","username")
remove_column("admin_users","salt")
rename_column("admin_users","hashed_password","password")
change_column("admin_users","email",:string,:default=>"",:null=>false)
remove_column("admin_users","username")
rename_table("admin_users","users")
end
end
3.use rake to up the databases structure
#rake db:migrate:up VERSION=20101117011002_alter_users.rb
4.use mysql command to see the changes
#mysql -u root -p
#use simple_cms_development;
#show columns from admin_users;
5.use rake to rollback
#rake db:migrate:down VERSION=20101117011002_alter_users.rb
#rails generate migrate AlterUsers
2.modify db/migrate/20101117011002_alter_users.rb
modify file like this
class AlterUsers < ActiveRecord::Migration
def self.up
rename_table("users","admin_users")
add_column("admin_users","username",:string,:limit=>25)
change_column("admin_users","email",:string,:limit=>100)
rename_column("admin_users","password","hashed_password")
add_column("admin_users","salt",:string,:limit=>40)
puts "*** about change table"
add_index("admin_users","username")
end
def self.down
remove_index("admin_users","username")
remove_column("admin_users","salt")
rename_column("admin_users","hashed_password","password")
change_column("admin_users","email",:string,:default=>"",:null=>false)
remove_column("admin_users","username")
rename_table("admin_users","users")
end
end
3.use rake to up the databases structure
#rake db:migrate:up VERSION=20101117011002_alter_users.rb
4.use mysql command to see the changes
#mysql -u root -p
#use simple_cms_development;
#show columns from admin_users;
5.use rake to rollback
#rake db:migrate:down VERSION=20101117011002_alter_users.rb
perform a testing migration
1.use rails generate to do a testing migration
#rails generate migration doNothingYet
it will generate one migration on db/migrate like
20101117002320_do_nothing_yet.rb
2.use rails generate new model user
#rails generate model user
it will generate new migration on db/migrate like
20101117002626_create_users.rb
3.modify the db/migrate/20101117002626_create_users.rb
add field to user table like
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string "first_name", :limit=>25
t.string "last_name", :limit=>50
t.string "email", :default=>"", :null=>false
t.string "password", :limit=>40
t.timestamps
end
end
def self.down
drop_table :users
end
end
3.running a migration
#rake db:migrate
4.use mysql to confirm user table create status
#mysql -u root -p
#user cms_development;
#show tables;
#show columns from users;
5.use ls to show migration version
#ls db/schema.rb
20101117002320_do_nothing_yet.rb 20101117002626_create_users.rb
to up the previous version
#rake db:migrate VERSION=20101117002320
or to specifical version up
#rake db:migrate:up VERSION=20101117002626
#rake db:migrate:down VERSION=20101117002626
#rake db:migrate:redo VERSION=20101117002626
#rails generate migration doNothingYet
it will generate one migration on db/migrate like
20101117002320_do_nothing_yet.rb
2.use rails generate new model user
#rails generate model user
it will generate new migration on db/migrate like
20101117002626_create_users.rb
3.modify the db/migrate/20101117002626_create_users.rb
add field to user table like
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string "first_name", :limit=>25
t.string "last_name", :limit=>50
t.string "email", :default=>"", :null=>false
t.string "password", :limit=>40
t.timestamps
end
end
def self.down
drop_table :users
end
end
3.running a migration
#rake db:migrate
4.use mysql to confirm user table create status
#mysql -u root -p
#user cms_development;
#show tables;
#show columns from users;
5.use ls to show migration version
#ls db/schema.rb
20101117002320_do_nothing_yet.rb 20101117002626_create_users.rb
to up the previous version
#rake db:migrate VERSION=20101117002320
or to specifical version up
#rake db:migrate:up VERSION=20101117002626
#rake db:migrate:down VERSION=20101117002626
#rake db:migrate:redo VERSION=20101117002626
connect mysql server and dump current database schema
1.modify database.yml to configure database connection
#vim config/database.yml
modify content like this
development:
adapter: mysql
encoding: utf8
database: cms_development
pool: 5
username: root
password: abc123
timeout: 5000
test:
adapter: mysql
encoding: utf8
database: cms_test
pool: 5
username: root
password: abc123
timeout: 5000
production:
adapter: mysql
encoding: utf8
database: cms_production
pool: 5
username: root
password: abc123
timeout: 5000
2.use rake to dump current migration
#rake db:schema:dump
it will generate schema.db on db folder
#vim config/database.yml
modify content like this
development:
adapter: mysql
encoding: utf8
database: cms_development
pool: 5
username: root
password: abc123
timeout: 5000
test:
adapter: mysql
encoding: utf8
database: cms_test
pool: 5
username: root
password: abc123
timeout: 5000
production:
adapter: mysql
encoding: utf8
database: cms_production
pool: 5
username: root
password: abc123
timeout: 5000
2.use rake to dump current migration
#rake db:schema:dump
it will generate schema.db on db folder
2010年11月15日 星期一
mysql basic syntax
1.list all database
show databases;
2.select one database
use mysql;
3.list all tables on database
show tables;
4.create new database
create database cms;
5.delete one database
drop database cms;
6.grant one user on all database with permission
grant all on *.* for 'username'@'host' identify by 'password';
flush privileges;
7.show permisson for specifial user
show grants for 'username'@'localhost';
show databases;
2.select one database
use mysql;
3.list all tables on database
show tables;
4.create new database
create database cms;
5.delete one database
drop database cms;
6.grant one user on all database with permission
grant all on *.* for 'username'@'host' identify by 'password';
flush privileges;
7.show permisson for specifial user
show grants for 'username'@'localhost';
rails environment
1.ruby on rails has three different environment
develpment for develop the application
test for unit testing use
production for online environment use
2.use config environment to custom the setting(mysql example)
#vim /cms/config/environment.rb
development:
adapter: mysql
encoding: utf8
reconnect: false
database: user_book
pool: 10
username: root
password: abc123
socket: /tmp/mysql.sock
develpment for develop the application
test for unit testing use
production for online environment use
2.use config environment to custom the setting(mysql example)
#vim /cms/config/environment.rb
development:
adapter: mysql
encoding: utf8
reconnect: false
database: user_book
pool: 10
username: root
password: abc123
socket: /tmp/mysql.sock
url parameter
1.indicate url parament
add new link on index web page
#vim /cms/app/views/demo/index.html.rb
<%= link_to('original web page',{:controller=>'demo',:action=>'index',:id=>'20',:page=>'5'}) %>
it will make url http://192.168.1.99/demo/index/20?page=5
2.use params to get value from url
add params to get parameters from url
#vim /cms/app/views/demo/index.html.rb
ID:<%= params['id'] %>
add new link on index web page
#vim /cms/app/views/demo/index.html.rb
<%= link_to('original web page',{:controller=>'demo',:action=>'index',:id=>'20',:page=>'5'}) %>
it will make url http://192.168.1.99/demo/index/20?page=5
2.use params to get value from url
add params to get parameters from url
#vim /cms/app/views/demo/index.html.rb
ID:<%= params['id'] %>
create redirect function to another page
1.user rediect_to functon on controller
#vim /cms/app/controller/demo_controller.rb
def index2
redirect_to(:action=>'say_Hello')
end
def index3
redirect_to('http://tw.yahoo.com')
end
def say_Hello
render(:text=>'welcome to use ruby on rails')
end
2.add say_Hello.html.rb on view page
add new line in it
<%=text %>
#vim /cms/app/controller/demo_controller.rb
def index2
redirect_to(:action=>'say_Hello')
end
def index3
redirect_to('http://tw.yahoo.com')
end
def say_Hello
render(:text=>'welcome to use ruby on rails')
end
2.add say_Hello.html.rb on view page
add new line in it
<%=text %>
create new hello html with instance variable and link to url
1.on deme controller add new function name hello
#vim /cms/app/controller/demo_controller.rb
add new function hell in the file
def hello
@firstArray = [1,2,3,4,5,6,7,8,9,10]
end
2.add new view on demo
#vim /cms/app/views/hello.html.rb
add new content
<% @firstArray.each do |x| %>
<%= x %><br />
<% end %>
<%= link_to('google at taiwan','http://www.google.com.tw/') %>
<br />
<%= link_to('index page','/demo/index') %>
<br />
<%= link_to('index page',{:action => 'index'}) %>
&
3.browser your web to http://192.168.1.99/demo/hello/
#vim /cms/app/controller/demo_controller.rb
add new function hell in the file
def hello
@firstArray = [1,2,3,4,5,6,7,8,9,10]
end
2.add new view on demo
#vim /cms/app/views/hello.html.rb
add new content
<% @firstArray.each do |x| %>
<%= x %><br />
<% end %>
<%= link_to('google at taiwan','http://www.google.com.tw/') %>
<br />
<%= link_to('index page','/demo/index') %>
<br />
<%= link_to('index page',{:action => 'index'}) %>
&
3.browser your web to http://192.168.1.99/demo/hello/
rails default route format
/:server/:controller/:action/:index
http://192.168.1.99/demo/index means
host: 192.168.1.99
controller: demo
action: index
http://192.168.1.99/demo/index means
host: 192.168.1.99
controller: demo
action: index
rails flow
If publice folder had fetch url folder
ruby on rails system will first access this folder
1.cd cms
2.cd public
3.mkdir testing
4.vim index.html
add one line to body tag
Welcome to use ruby on rails 3 framework
5.run rails server
#rails server
6.use browser to access the url
http://192.168.1.99:3000/testing/index/
ruby on rails system will first access this folder
1.cd cms
2.cd public
3.mkdir testing
4.vim index.html
add one line to body tag
Welcome to use ruby on rails 3 framework
5.run rails server
#rails server
6.use browser to access the url
http://192.168.1.99:3000/testing/index/
create new controller
1.use rails to create controller
#rails generate controller demo index
it will create demo_controller and first index function
2.run server to confirm demo_controller and index function is ok
#rails server -b 192.168.1.99 -p 3000
3.use browser to indicate the url is ok
http://192.168.1.99:3000/demo/index/
#rails generate controller demo index
it will create demo_controller and first index function
2.run server to confirm demo_controller and index function is ok
#rails server -b 192.168.1.99 -p 3000
3.use browser to indicate the url is ok
http://192.168.1.99:3000/demo/index/
2010年11月14日 星期日
create new project on rails
1.use rails to create new project name cms
#rails cms -D mysql
2.list all sub folder on cms
#cd cms
#ls -al
3.rails sub folder archtecture
app: Model View Controller folder location
config:database,application and environment configuration
db: data location
#rails cms -D mysql
2.list all sub folder on cms
#cd cms
#ls -al
3.rails sub folder archtecture
app: Model View Controller folder location
config:database,application and environment configuration
db: data location
install rails environment
how to install ruby on rails 3 on ubuntu LTS 10.04
1.install below new package
#sudo apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic libsqlite3-dev mysql-server mysql-client libmysql-ruby libmysqlclient-dev
2.download ruby 1.9.2 package and install it
#sudo tar zxvf ruby-1.9.2-p0.tar.gz
#cd ruby-1.9.2-p0
#./configure
#make
#make install
3.add /usr/local/bin/ruby to /etc/environment
#vim /etc/environment
#source /etc/environment
4.install rubygems
#sudo apt-get install rubygems
5.gem install mysql
#gem install mysql
6.gem install sqlite3-ruby
# gem install sqlite3-ruby
1.install below new package
#sudo apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic libsqlite3-dev mysql-server mysql-client libmysql-ruby libmysqlclient-dev
2.download ruby 1.9.2 package and install it
#sudo tar zxvf ruby-1.9.2-p0.tar.gz
#cd ruby-1.9.2-p0
#./configure
#make
#make install
3.add /usr/local/bin/ruby to /etc/environment
#vim /etc/environment
#source /etc/environment
4.install rubygems
#sudo apt-get install rubygems
5.gem install mysql
#gem install mysql
6.gem install sqlite3-ruby
# gem install sqlite3-ruby
訂閱:
文章 (Atom)