note: ini juga nulis emailnya tidak sempat tuntas terus, saya kirim sahaja , sudah saved sejak 15 Dec , khawatir malah akan saya discard mesej nya. intinya saya disini memberi alternatif beda dengan tujuan sama, kalau memang dasar mentok baru boleh pakai cara tradisional menggunakan tanda tanya itu (bukan REST dan not much friendly to google mimiking railers yg suka pada nyolot sama seasidenya smalltalk)
oke setelah membaca penjelasan anda di email yang lain saya menjadi faham dan ingin menjawab di dalam thread ini aja ya. On 14/12/2007, Hadi Sunyoto <[EMAIL PROTECTED]> wrote: > Apakah cara yang terbaik untuk melakukan parameter passing untuk view dan > controller > > misal (bayangkan code scaffolding) > http://localhost/product/list?show_all=true > > dan di list itu ada EDIT link, yang notabene bakal mengedit product > http://localhost/product/edit/1, dan setelah edit bakal dikembalikan ke page > list.. > bagaimana meminta ruby on rails untuk menulis > http://localhost/product/list?show_all=true lagi? > > Kalau langsung hardcode sih kayaknya error-prone deh, lagian tidak flexible, > kalau page list nya ternyata http://localhost/product/list?show_all=false, > harusnya juga waktu return dari edit tetap keluar > http://localhost/product/list?show_all=false, bukan > http://localhost/product/list?show_all=true > > Cara yang terpikir sekarang ini sih dengan menyimpan hasil param dan di pass > terus > > def list > @show_all = params[:show_all] > ... > end > > def edit > @show_all = params[:show_all] > ... > redirect_to 'list', .... beserta option optionnya > end > > tapi kesannya koq sama sekali tidak elegan.. kalau misalnya params-nya ada > banyak kan malah membingungkan. > > Pasti ada cara yang lebih bagus peraturan railers dan ini juga yang menjadi alasan mengapa orang ruby jauh berkali lipat lebih produktif ketimbang yang lain. 1. sebagai developer berjuanglah pada kesederhanaan. bila ada fitur yang diinginkan itu fancy lihat dulu, apakah ada alternatif yang simpel dalam artian tak redundant, elegant, dan dapat diselesaikan sekejap. 2. berpikir secara resources. dari keperluan yang diinginkan spesifikasi requirement berarti : it "should list all products" it "should list 10 top products" setidaknya begitulah yang saya tangkap dan fahami daripada penjelasan yang sdh Anda beri di email terakhir. Sekarang setelah mengetahui keperluan apa aja yang ingin dipenuhi, maka terjemahkan dari perbendaharaan kata-kata orang bisnis itu ke spesifik domain yang sederhana, yang ada adalah products. jadi GET /products GET /products/top GET /products/:id/edit PUT /products/:id kemudian berangkat dari sini Anda tinggal tuliskan spec Anda sebelum Anda memulai coding. PB-G4-15:Desktop arie$ rails products -d sqlite3 create create app/controllers create app/helpers [...] PB-G4-15:Desktop arie$ cd products PB-G4-15:products arie$ script/plugin sources http://dev.rubyonrails.com/svn/rails/plugins/ PB-G4-15:products arie$ script/plugin source svn://rubyforge.org/var/svn/rspec/trunk Added 1 repositories. PB-G4-15:products arie$ script/plugin install rspec && script/plugin install rspec_on_rails && script/generate rspec && script/generate rspec_model Product name:string sold:integer && script/generate rspec_controller products PB-G4-15:products arie$ cat config/database.yml # SQLite version 3.x # gem install sqlite3-ruby development: adapter: sqlite3 database: db/development.sqlite3 timeout: 5000 # Warning: The database defined as 'test' will be erased and # re-generated from your development database when you run 'rake'. # Do not set this db to the same as development or production. test: adapter: sqlite3 database: db/test.sqlite3 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 timeout: 5000 PB-G4-15:products arie$ rake -T all (in /Users/arie/Desktop/products) rake db:create:all # Create all the local databases defined in config/database.yml rake db:drop:all # Drops all the local databases defined in config/database.yml PB-G4-15:products arie$ rake db:create:all (in /Users/arie/Desktop/products) "db/development.sqlite3 already exists" "db/production.sqlite3 already exists" "db/test.sqlite3 already exists" PB-G4-15:products arie$ ls -lh db total 0 -rw-r--r-- 1 arie staff 0B Dec 15 03:47 development.sqlite3 drwxr-xr-x 2 arie staff 102B Dec 15 03:46 migrate -rw-r--r-- 1 arie staff 0B Dec 15 03:47 production.sqlite3 -rw-r--r-- 1 arie staff 0B Dec 15 03:47 test.sqlite3 PB-G4-15:products arie$ PB-G4-15:products arie$ cat db/migrate/001_create_products.rb class CreateProducts < ActiveRecord::Migration def self.up create_table :products do |t| t.string :name t.integer :sold t.timestamps end end def self.down drop_table :products end end PB-G4-15:products arie$ rake db:migrate (in /Users/arie/Desktop/products) == 1 CreateProducts: migrating ================================================ -- create_table(:products) -> 0.0095s == 1 CreateProducts: migrated (0.0116s) ======================================= PB-G4-15:products arie$ rake db:test:prepare (in /Users/arie/Desktop/products) PB-G4-15:products arie$ berhenti sekejap ingin liat rake stats walau belum menyentuh codes project rails kali ini PB-G4-15:products arie$ rake stats (in /Users/arie/Desktop/products) +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 12 | 6 | 2 | 0 | 0 | 0 | | Helpers | 5 | 4 | 0 | 0 | 0 | 0 | | Models | 2 | 2 | 1 | 0 | 0 | 0 | | Libraries | 0 | 0 | 0 | 0 | 0 | 0 | | Model specs | 11 | 9 | 0 | 0 | 0 | 0 | | View specs | 0 | 0 | 0 | 0 | 0 | 0 | | Controller specs | 10 | 6 | 0 | 0 | 0 | 0 | | Helper specs | 11 | 7 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 51 | 34 | 3 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 12 Test LOC: 22 Code to Test Ratio: 1:1.8 PB-G4-15:products arie$ oke, perbandingan code to test ratio nya satu banding hampir dua ya. oke kita lanjut: sekarang kita lakukan perubahan sedikit di spec_controller ya berikut ini adalah file spec/controllers/products_controller_spec.rb require File.dirname(__FILE__) + '/../spec_helper' # GET /products describe ProductsController, "index" do it "should render template index" it "should assigns products listings" end # GET /products/top describe ProductsController, "top" do it "should render template top" it "should assign top products to list" end # GET /products/:id/edit describe ProductsController, "edit" do it "should render template edit" it "should assign product to edit" end # PUT /products/:id describe ProductsController, "update" do it "should update a product" it "should assign a notice and redirect to ... upon success" it "should render template edit back upon failure" end kemudian setelah ini (aduuuh kepanjangan ya ini spec , maaf karena saya kurang sabar untuk menuliskannya satu per satu dan codes bergantian sambil copy paste ke textarea gmail saya / kalau siaran langsung baru boleh tak macam ni). lalu kembali lagi ke konsole dan jalankan rake spec (untuk selanjutnya saya menyalakan autotest supaya saya tak perlu menjalankan rake spec terus-terusan atau pun menekan apple r untuk mencek apakah code saya sudah memenuhi testing atau belum). PB-G4-15:products arie$ rake spec (in /Users/arie/Desktop/products) ..PPPPPPPPP Pending: ProductsController update should render template edit back upon failure (Not Yet Implemented) ProductsController update should assign a notice and redirect to ... upon success (Not Yet Implemented) ProductsController update should update a product (Not Yet Implemented) ProductsController edit should assign product to edit (Not Yet Implemented) ProductsController edit should render template edit (Not Yet Implemented) ProductsController top should assign top products to list (Not Yet Implemented) ProductsController top should render template top (Not Yet Implemented) ProductsController index should assigns products listings (Not Yet Implemented) ProductsController index should render template index (Not Yet Implemented) Finished in 0.394218 seconds 11 examples, 0 failures, 9 pending PB-G4-15:products arie$ ya, ok, semua berjalan semestinya, karena memang kita baru menorehkan spesifikasi saja dari orang bisnis dan juga bisa dibaca oleh orang bisnis ke file spec kita. kemudian mari kita lanjutkan spec nya agar membentuk codes sesuai spesifikasi spec ya (BTW rspec cara bacanya RESPECT!!) ok, lanjut, PB-G4-15:products arie$ autotest -bash: autotest: command not found PB-G4-15:products arie$ gem list *** LOCAL GEMS *** actionmailer (2.0.1, 1.3.5, 1.3.3) actionpack (2.0.1, 1.13.5, 1.13.3) actionwebservice (1.2.5, 1.2.3) activerecord (2.0.1, 1.15.5, 1.15.3) activeresource (2.0.1) activesupport (2.0.1, 1.4.4, 1.4.2) acts_as_ferret (0.4.1) BlueCloth (1.0.0) builder (2.1.2) capistrano (2.0.0) cgi_multipart_eof_fix (2.5.0, 2.2) daemons (1.0.7) dnssd (0.6.0) fastri (0.3.0.1) fastthread (1.0.1, 1.0) fcgi (0.8.7) feedtools (0.2.26) ferret (0.11.4) gem_plugin (0.2.3, 0.2.2) highline (1.2.9) hoe (1.3.0) hpricot (0.6) htmlentities (4.0.0) libxml-ruby (0.3.8.4) mongrel (1.0.1) needle (1.3.0) net-sftp (1.1.0) net-ssh (1.1.2) piston (1.3.3) radiant (0.6.4) rails (2.0.1, 1.2.5, 1.2.3) rake (0.7.3) RedCloth (3.0.4) rmagick (1.15.10) rspec (1.0.8) ruby-openid (1.1.4) ruby-yadis (0.3.4) rubyforge (0.4.4) rubygems-update (0.9.5) rubynode (0.1.3) shorturl (0.8.2) sources (0.0.1) sqlite3-ruby (1.2.1) termios (0.9.4) twitter (0.2.1) twitterize (1.0.0) uuidtools (1.0.2) PB-G4-15:products arie$ gem search autotest *** LOCAL GEMS *** PB-G4-15:products arie$ gem search autotest -r *** REMOTE GEMS *** Bulk updating Gem source index for: http://gems.rubyforge.org PB-G4-15:products arie$ gem search zentest -r *** REMOTE GEMS *** ZenTest (3.6.1, 3.6.0, 3.5.2, 3.5.1, 3.4.3, 3.4.2, 3.4.1, 3.4.0, 3.3.0, 3.2.0, 3.1.0, 3.0.0) zentest (3.5.0) PB-G4-15:products arie$ sudo gem install ZenTest Password: Successfully installed rake-0.7.3 Successfully installed ZenTest-3.6.1 2 gems installed Installing ri documentation for rake-0.7.3... Installing ri documentation for ZenTest-3.6.1... Installing RDoc documentation for rake-0.7.3... Installing RDoc documentation for ZenTest-3.6.1... PB-G4-15:products arie$ which autotest /usr/bin/autotest PB-G4-15:products arie$ ya, kalau belum ada autotest nya pasang dulu. kalau sudah nyala kita lanjut. PB-G4-15:products arie$ autotest loading autotest/rails_rspec /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S script/spec -O spec/spec.opts spec/models/product_spec.rb spec/controllers/products_controller_spec.rb spec/helpers/products_helper_spec.rb ..PPPPPPPPP Pending: ProductsController update should render template edit back upon failure (Not Yet Implemented) ProductsController update should assign a notice and redirect to ... upon success (Not Yet Implemented) ProductsController update should update a product (Not Yet Implemented) ProductsController edit should assign product to edit (Not Yet Implemented) ProductsController edit should render template edit (Not Yet Implemented) ProductsController top should assign top products to list (Not Yet Implemented) ProductsController top should render template top (Not Yet Implemented) ProductsController index should assigns products listings (Not Yet Implemented) ProductsController index should render template index (Not Yet Implemented) Finished in 0.463075 seconds 11 examples, 0 failures, 9 pending ya. kemudian saya melakukan perubahan di file spec (masih di file spec controller yang sama ya / untuk save my poor finger saya akan singkat ngan sebut file spec aja) # GET /products describe ProductsController, "index" do it "should render template index" do load_page response.should render_template(:index) end it "should assigns products listings" def load_page(params={}) defaults = {} get :index, defaults.merge!(params) end end ya, berfokus di file index, maka hasil dari autotest adalah sbb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S script/spec -O spec/spec.opts spec/controllers/products_controller_spec.rb PPPPPPPPF Pending: ProductsController update should render template edit back upon failure (Not Yet Implemented) ProductsController update should assign a notice and redirect to ... upon success (Not Yet Implemented) ProductsController update should update a product (Not Yet Implemented) ProductsController edit should assign product to edit (Not Yet Implemented) ProductsController edit should render template edit (Not Yet Implemented) ProductsController top should assign top products to list (Not Yet Implemented) ProductsController top should render template top (Not Yet Implemented) ProductsController index should assigns products listings (Not Yet Implemented) 1) ActionController::UnknownAction in 'ProductsController index should render template index' No action responded to index /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/filters.rb:697:in `call_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:293:in `measure' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/rescue.rb:199:in `perform_action_without_caching' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/caching.rb:678:in `perform_action' /Library/Ruby/Gems/1.8/gems/activerecord-2.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache' /Library/Ruby/Gems/1.8/gems/activerecord-2.0.1/lib/active_record/query_cache.rb:8:in `cache' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/caching.rb:677:in `perform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/base.rb:524:in `send' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/base.rb:524:in `process_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/filters.rb:685:in `process_without_session_management_support' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/session_management.rb:123:in `process_without_test' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/test_process.rb:15:in `process' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/test_process.rb:393:in `process' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.1/lib/action_controller/test_process.rb:364:in `get' ./spec/controllers/products_controller_spec.rb:14:in `load_page' ./spec/controllers/products_controller_spec.rb:6: /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_methods.rb:79:in `instance_eval' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_methods.rb:79:in `run_with_description_capturing' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/matchers.rb:144:in `capture_generated_description' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_methods.rb:78:in `run_with_description_capturing' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_methods.rb:19:in `execute' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:48:in `timeout' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_methods.rb:16:in `execute' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:280:in `execute_examples' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:279:in `each' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:279:in `execute_examples' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:120:in `run' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:22:in `run' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:in `each' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:in `run' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/runner/options.rb:87:in `run_examples' /Users/arie/Desktop/products/vendor/plugins/rspec/lib/spec/runner/command_line.rb:19:in `run' script/spec:4: Finished in 0.370913 seconds 9 examples, 1 failure, 8 pending dilihat dari error message nya tentu saja salah / masih error karena memang kita belum koding sama sekali / baru ngetes2x. oke, coba kita lihat apa errornya. ooooh no action responded to index to... ya sudah kita bikinkan. liat file app/controllers/products_controller.rb ya sekarang setelah melakukan perubahan sedikit, dengan hanya menambahkan: def index end saja sudah membuat testing pass / sukses. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S script/spec -O spec/spec.opts spec/controllers/products_controller_spec.rb PPPPPPPP. Pending: ProductsController update should render template edit back upon failure (Not Yet Implemented) ProductsController update should assign a notice and redirect to ... upon success (Not Yet Implemented) ProductsController update should update a product (Not Yet Implemented) ProductsController edit should assign product to edit (Not Yet Implemented) ProductsController edit should render template edit (Not Yet Implemented) ProductsController top should assign top products to list (Not Yet Implemented) ProductsController top should render template top (Not Yet Implemented) ProductsController index should assigns products listings (Not Yet Implemented) Finished in 0.374354 seconds 9 examples, 0 failures, 8 pending maka kita lanjut ke bagian it/specify selanjutnya. # GET /products describe ProductsController, "index" do it "should render template index" do load_page response.should render_template(:index) end it "should assigns products listings" do load_page assigns[:products].should == @products end def load_page(params={}) defaults = {} get :index, defaults.merge!(params) end end ya, perubahan pada assigns itu, pasti error lah autotestnya : -- Arie | http://linkedin.com/in/ariekeren | http://profile.to/ariekeren/ http://ariekusumaatmaja.wordpress.com | http://groups.yahoo.com/groups/id-ruby

