What is MVC in Ruby on Rails?

2019-01-10 11:51发布

Could someone please explain MVC to me in Ruby on Rails, in layman terms. I am especially interested in understanding the Model in MVC (can't get my head around the model).

8条回答
劳资没心,怎么记你
2楼-- · 2019-01-10 11:57

Here's a brief overview at a high level on how the MVC Pattern works:

Controller:

  1. Listens on some kind of interaction/event stream.
  2. Controller can send the model that type of interaction/event.
  3. Controller can also communicate with the the view.

Model:

  1. Models will listen in on the interaction/event from the controller.
  2. Is an abstraction of a data source.
  3. Handles data logic and manipulation.
  4. After it is done with logic, it then sends to controller which will then communicate with the view.

View:

  1. View can communicate with the controller.
  2. Knows how to render data from the Model to the browser visually.
  3. The Controller tells to View to do something with something from the Model.

A couple of things to note is that models can't communicate with views directly and vise versa. Only the controller can communicate with the view and model, so the controller acts as the delegator for the interaction/event retrieved from users interaction on the browser.

check this link for more clear understanding

one more link to get clear

查看更多
Ridiculous、
3楼-- · 2019-01-10 11:59

Ruby on Rails does not implement the MVC design pattern. Ruby on Rails has folders called controllers, models, and views. The views folder has HTML files. The controllers and models folder have ruby files. The controllers map to a URL and a method in the controller gets executed upon requesting that URL, the associated view (HTML file) gets loaded and the model (data structure) for it is used to populate the view. Thats the extent of it's likeness to the MVC design pattern. It's a shame that people say it's MVC because it's caused a generation of confusion and misunderstanding of the MVC design pattern.

In Rails, the model is a data structure.

查看更多
放荡不羁爱自由
4楼-- · 2019-01-10 12:02

I think the best way to wrap your head around MVC is by example. Try coding up a simple Rails app using MVC. There are many tutorials online, such as the blog example at "Getting Started with Rails".

If chose to learn by coding an example, check out the answers to Where can I find clear examples of MVC?

查看更多
Fickle 薄情
5楼-- · 2019-01-10 12:02

MVC isn't specifically just for Ruby on Rails. It was actually created awhile before Ruby on Rails ever came around. It's mainly just a way of organizing your code so that you have some code that's responsible for your models (the Class version of a database table), some code that's responsible for your views (what's visually being displayed to the user) and code that's responsible for your controllers (what ties the views to the models and performs the bulk of your logic.

That's the non-framework-specific description. Each framework that uses MVC has a different way of implementing it. For Ruby on Rails each model represents a database table as a class that can communicate directly in code with other objects without needing to write any SQL. All the SQL is being taken care of in the background and you just have to think of it as though it were a normal class (well almost, it's not seamless yet). The view is mostly HTML and represents what will be sent to the browser. The controller is just the code that communicates the models and views together.

All in all, MVC isn't specific just to Ruby on Rails...that's just the most popular.

查看更多
一夜七次
6楼-- · 2019-01-10 12:07

enter image description here

MVC basically indicates Model-View-Controller. And MVC used by many languages like PHP, Perl, Python etc. Generally MVC works like this:

Request first comes to the controller, controller finds and appropriate view and interacts with model, model interacts with your database and send the response to controller then controller based on the response give the output parameter to view.

查看更多
混吃等死
7楼-- · 2019-01-10 12:07

The Model View Controller principle divides the work of an application into 3 separate but closely cooperative subsystems.

Model (ActiveRecord ):

It maintains the relationship between the objects and the 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.

View ( ActionView ):

It is a presentation of data in a particular format, triggered by a controller's decision to present the data. They are script-based template 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.

Controller ( ActionController ):

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, messaging 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).

Check the links below for clear understanding of mvc in rails:

http://www.bogotobogo.com/RubyOnRails/RubyOnRails_Model_View_Controller_MVC.php

https://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/

查看更多
登录 后发表回答