What is MVC in Ruby on Rails?

后端 未结 8 1664
情深已故
情深已故 2020-12-02 09:06

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条回答
  • 2020-12-02 09:28

    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.

    0 讨论(0)
  • 2020-12-02 09:28

    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?

    0 讨论(0)
  • 2020-12-02 09:36

    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.

    0 讨论(0)
  • 2020-12-02 09:40

    Your Model is the data structure that your program uses.

    The View is the part that interacts with the screen or the next level up.

    The Controller generally processes data between the model and view

    MVC structures are often nested, so a "Model" or "View" may contain its own MVC (Think of a component on the screen. You may just fill it with a string, but behind the scenes the code of the component draws its own little view, has it's own little model (the string you pass in) and has a little controller drawing the data onto the view.

    In Rails, the roles of the model, view and controller are well-defined by the framework, any tutorial will point out the three components as it walks you through the files it created.

    In other systems, those pieces may be harder to identify. Also, MVC is not "Perfect", just keep in mind that there are valid alternatives, but it's a good way to start organizing.

    0 讨论(0)
  • 2020-12-02 09:40

    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

    0 讨论(0)
  • 2020-12-02 09:41

    Some background, MVC is a (compound) design pattern and was developed in 1979 by Trygve Reenskaug (Smalltalk).

    True MVC was primarily planned for use in n-tier (non web) systems and it splits a system into 3 distinct parts, a Model, View and Controller

    The Model

    • Contains data for the application (often linked to a database)
    • Contains state of the application (e.g. what orders a customer has)
    • Contains all business logic
    • Notifies the View of state changes (** not true of ROR, see below)
    • No knowledge of user interfaces, so it can be reused

    The View

    • Generates the user interface which presents data to the user
    • Passive, i.e. doesn’t do any processing
    • Views work is done once the data is displayed to the user.
    • Many views can access the same model for different reasons

    The Controller

    • Receive events from the outside world (usually through views)
    • Interact with the model
    • Displays the appropriate view to the user

    ** Classic MVC is not suited to web applications, as the model cannot send all changes to the view in an observer fashion (the view is a web page). The Model2 was introduced to overcome the changing infrastructure by JSP team in 90s . MVC Web frameworks are really not MVC, but Model2 (this is true of Ruby on Rails).

    Here is a description of GUI patterns including MVC from the master, Martin Fowler GUI Architectures

    The best book I have found so far is Agile Web Development with Rails. It begins by assuming no knowledge, and is quite comprehensive.

    Hope this helps to shed some light for you!

    0 讨论(0)
提交回复
热议问题