Ruby on Rails menggunakan konsep MVC sebagai design pattern aplikasinya. Keuntungan yang berusaha diperoleh dengan MVC : * pemisahan bussiness logic dari user interface * memudahkan control kepada kode agar tidak berulang (DRY) * memisahkan kode-kode yang berbeda agar memudahkan maintenance Model Model : Maintains the relationship between Object and Database and handles validation, association, transactions, and more. This subsystem is implemented in ActiveRecord library which provides an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables, and so on. A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, each table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.
View View : A presentation of data in a particular format, triggered by a controller’s decision to present the data. They are script based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX technology. This subsystem is implemented in ActionView library which is an Embedded Ruby (ERb) based system for defining presentation templates for data presentation. Every Web connection to a Rails application results in the displaying of a view. Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.
Controller Controller : The facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organizing that data (searching, sorting, massaging it) into a form that fits the needs of a given view. This subsystem is implemented in ActionController which is a data broker sitting between ActiveRecord (the database interface) and ActionView (the presentation engine). Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
Pemrosesan Request
Bagaimana request diproses di Rails MVC:
- Browser (client) mengakses suatu URL, misalnya aplikasi.com/product/edit/1
- Dengan kata lain browser mengirimkan request kepada web/application server(Mongrel, WebRick, Passenger)
- Web Server menerima request tersebut dan menggunakan ROUTES untuk mengenali bagian apakah yang coba diakses
- Dalam contoh ini berarti akan mengakses CONTROLLER product, ACTION edit dengan ID 1
- Web server kemudian menggunakan DISPATCHER untuk mengakses controller dan action yang sesuai beserta paramater id nya
- Controller product melakukan pekerjaannya dengan memparsing request user, data yang di submit, cookies dan session apakah sesuai
- Dalam contoh ini berarti Controller meminta pada Model data product id 1 yang siap untuk ditampilkan, dan meminta pada View untuk menyiapkan container tampilan/UI data product id 1 sesuai styling yang dimiliki View tersebut
- Controller mengirimkan response(body dan metadata) kepada web server
- Web server mengkombinasikan response itu menjadi tampilan yang akhirnya bisa dinikmati user di browsernya
Tinjauan — 1. http://guides.rubyonrails.org/getting_started.html 2. http://www.tutorialspoint.com/ruby-on-rails/rails-framework.htm 3. http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ 4. http://slash7.com/2005/02/22/mvc-the-most-vexing-conundrum/ 5. http://www.slideshare.net/codeinmotion/mvc-demystified-essence-of-ruby-on-rails
