Jan 30 2008
Depot Project With Rails 2.0.2 (1)
Mumpung masih hangat nih, aku pengen bikin dokumentasi untuk depot project dengan menggunakan rails versi 2.0.2. Pastikan semua paket pendukung telah terinstall. Jika semua sudah siap, langsung saja kita mulai projectnya. Sekarang kita buat projectnya dulu dengan menggunakan perintah:
[root@otid htdocs]# rails -d mysql depot
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/performance/request
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
[root@otid htdocs]# cd depot
[root@otid depot]#
Berikutnya jika diperlukan ubah konfigurasi database pada config/database.yml
development:
adapter: mysql
encoding: utf8
database: coba_development
username: root
password:
socket: /opt/lampp/var/mysql/mysql.sock
Kemudian kita buat database dengan command :
[root@otid depot]# rake db:create
(in /opt/lampp/htdocs/depot)
[root@otid depot]#
Secara otomatis database telah terbuat. Nama database secara default akan di load dari file database.yml (dalam kasus ini adalah database: depot_development). Kemudian jalankan server untuk rails, pada kesempatan kali ini aku pake WEBrick sebagai server untuk rails-nya.
[root@otid depot]# script/server
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with –help for options
[2008-01-30 16:56:15] INFO WEBrick 1.3.1
[2008-01-30 16:56:15] INFO ruby 1.8.6 (2007-09-24) [i386-linux]
[2008-01-30 16:56:15] INFO WEBrick::HTTPServer#start: pid=4065 port=3000
Kemudian jalankan command berikut :
[root@otid depot]# script/generate scaffold product title:string image_url:string description:text
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/products
exists app/views/layouts/
exists test/functional/
exists test/unit/
create app/views/products/index.html.erb
create app/views/products/show.html.erb
create app/views/products/new.html.erb
create app/views/products/edit.html.erb
create app/views/layouts/products.html.erb
create public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/product.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/001_create_products.rb
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
route map.resources :products
[root@otid depot]#
Dengan kita generate scaffold product, maka secara otomatis rails akan membuatkan file untuk input, edit, maupun delete data (atau lebih tepatnya membuat controller product, model product dan file untuk migration). Kemudian kita lihat pada migration file (db/migrate/001_create_products.rb).
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title, :image_url
t.text :descriptiont.timestamps
end
enddef self.down
drop_table :products
end
end
Maksud dari script “t.string :title” adalah rails akan membuat satu field pada tabel products bernama title dan bertipe data string(varchar), secara default pula akan memilih input tipe text pada form. Sedangkan untuk tipe text menggunakan textarea pada editornya. Berikut contohnya (app/views/products/new.html.erb).
<h1>New product</h1>
<%= error_messages_for :product %>
<% form_for(@product) do |f| %>
<p>
<b>Title</b><br />
<%= f.text_field :title %>
</p><p>
<b>Image url</b><br />
<%= f.text_field :image_url %>
</p><p>
<b>Description</b><br />
<%= f.text_area :description %>
</p><p>
<%= f.submit “Create” %>
</p>
<% end %><%= link_to ‘Back’, products_path %>
Kemudian buat table dengan menjalankan command :
[root@otid depot]# rake db:migrate
(in /opt/lampp/htdocs/depot)
== 1 CreateProducts: migrating ================================================
– create_table(:products)
-> 0.0222s
== 1 CreateProducts: migrated (0.0224s) =======================================[root@otid depot]#
Pastikan tabel products telah terbentuk. Berikutnya jika kita ingin menambahkan field lagi (misal: price), kita bisa menggunakan cara berikut :
[root@otid depot]# script/generate migration add_price_to_products price:decimal
exists db/migrate
create db/migrate/002_add_price_to_products.rb
[root@otid depot]#
Kemudian kita buka file: db/migrate/002_add_price_to_products.rb tambahkan beberapa script sehingga bisa kita lihat seperti berikut :
class AddPriceToProducts < ActiveRecord::Migration
def self.up
add_column :products, :price, :decimal, :precision => 8, :scale =>2, :default =>0
enddef self.down
remove_column :products, :price
end
end
Jalankan lagi command “rake db:migrate”.
[root@otid depot]# rake db:migrate
(in /opt/lampp/htdocs/depot)
== 2 AddPriceToProducts: migrating ============================================
– add_column(:products, :price, :decimal, {:default=>0, :scale=>2, :precision=>8})
-> 0.1259s
== 2 AddPriceToProducts: migrated (0.1265s) ===================================[root@otid depot]#
Dengan demikian field price dengan tipe desimal sudah diperoleh. Sekarang kita tambahkan field price pada form new dan edit. Kita dapat mengubahnya menjadi seperti berikut :
<h1>New product</h1>
<%= error_messages_for :product %>
<% form_for(@product) do |f| %>
<p>
<b>Title</b><br />
<%= f.text_field :title %>
</p><p>
<b>Price</b><br />
<%= f.text_field :price %>
</p><p>
<b>Image url</b><br />
<%= f.text_field :image_url %>
</p><p>
<b>Description</b><br />
<%= f.text_area :description %>
</p><p>
<%= f.submit “Create” %>
</p>
<% end %><%= link_to ‘Back’, products_path %>
Berikutnya kita buka model product (app/models/product.rb). File ini berfungsi untuk melakukan validasi-validasi variabel yang terkirim dari form baik itu form untuk buat baru maupun edit data. Setelah validasi ditambahkan, hasilnya bisa seperti berikut :
class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validates_format_of :image_url,
:with => %r{\.(gif|jpg|png)$}i,
:message => ” must be a URL for a GIF, JPG, or PNG image”protected
def validate
errors.add(:price, “should be at least 0.01″) if price.nil? || price < 0.01
end
end
Saat pengiriman variabel dari form, akan terjadi beberapa pengecekan di file ini. Misalnya, ada tidaknya data pada judul, image_url, dan description. Terjadi pengecekan untuk price dan ke-unique-an title serta validasi tipe data dari image_url. Setelah itu kita bisa cek di http://localhost:3000/products/new. Kurang lebih akan tampil seperti gambar di bawah :

Gambar di atas adalah form input untuk mengisi data pada project depot kali ini. Setelah kita klik button Create di bagian bawah, kemudian akan terjadi pengecekan beberapa variabel yang di kirim oleh model product (app/models/product.rb). Setelah semua validasi ok, proses berikutnya adalah insert data ke database. Untuk edit data dan delete data aku rasa semua dah bisa di coba masing-masing. Aku rasa sementara sampai di sini dulu, lain kali kita lanjutkan lagi.
Good Luck!
2 Responses to “Depot Project With Rails 2.0.2 (1)”
Leave a Reply
You can also use this smilies by clicking them :
more » |














Hebat Ueyyy,,boice boice kang
Aduuuh …
kk klo saya nambah 5 fields untuk setiap table,
dan table saya 200 buah….
cara yang gampang gimana yaaa