如何连接模型,视图和控制器? [英] How are Model, View and Controller connected?

查看:109
本文介绍了如何连接模型,视图和控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解模型,视图和控制器在应用程序中扮演的不同角色,但是它们如何相互链接,例如,一个简单的比较是将JavaScript文件与HTML文件链接时,需要使用'src标签'参考.

I understand the different roles the Model, View and Controller play in an application but how are they linked to each other, for example a simple comparison is when linking a JavaScript file with an HTML file a tag is required with a 'src' reference.

自从学习Rails以来,我被告知它们是链接的,但没有联系.

Since learning Rails I have been told they are linked, but not how.

实际上如何链接模型,视图和控制器,为什么MVC(特别是Rails)不需要'src'或类似的东西?

How are the Model, View and Controller actually linked and why is a 'src' or something similar not required with MVC and specifically Rails?

推荐答案

基本上,它们都是通过路由控制器和动作链接的.

Basically it's all linked through routes controllers and actions.

routes文件(your_app_root/config中的routes.rb文件)有效地拦截传入的URL并将该URL映射到控制器/动作(实际上是由Nginx之类的Web服务器完成的,然后将请求传递到Rails应用程序通过诸如独角兽之类的东西,但这完全是另一个问题).

The routes file (routes.rb file in your_app_root/config) effectively intercepts an incoming url and maps that url to a controller/action (actually that's done by a web server like Nginx which then passes the request on to your rails app via something like Unicorn but that's another question entirely).

因此,对于标准的HTML网站设置,您可能会有一个名为home的文件夹,并且在该文件夹中可能会有一个index.html

So for a standard HTML site set up you might have a folder called home and in that folder you might have an index.html

因此,当您导航到some_url/home/index时,您将获得在浏览器中呈现的index.html文件的内容.

So when you navigate to some_url/home/index then you will get the contents of the index.html file rendered in the browser.

在RoR中,要使用此路由,您将需要一条路由,该路由定义了对集合的获取请求(传入的参数为多个或没有传入参数)

In RoR for this to work you will need a route defining a get request for a collection (plural or no params passed in)

它可能看起来像这样

YourApp::Application.routes.draw do
  get 'home', to: 'home#index', as: :home
  # etc...

如果您导航到some_url/home,则该路由会将您的浏览器连接到home控制器上的index操作

That route would connect your browser to the index action on the home controller if you navigate to some_url/home

home控制器中的index动作只能是一个空动作

The index action in the home controller can just be an empty action

class HomeController < ApplicationController
    def index
    end
end

并在app/views/home文件夹中提供index.html.erb,然后该文件将自动呈现,因为ActionController会将您告诉它的任何内容转换为HTML,css和javascript或json或XML如果您使用的是资源路由,并且接收到的请求是XML或JSON请求,然后将结果数据发送回浏览器

And providing you have an index.html.erb in the app/views/home folder then you will get that file rendered automatically as ActionController translates whatever you have told it to render in to HTML, css and javascript or json or XML if you are using resource routes and the request it receives is an XML or JSON request, and sends the resulting data back to the browser

如果您想从数据库中获取一些数据,则控制器操作负责获取该数据并将其填充到一个对象(视图变量可以用@符号表示)中,该对象可以在视图中使用erb标签

If you wanted to get some data displayed from your database then the controller action has the responsibility of obtaining that data and stuffing it into an object (an instance variable which is denoted by an @ symbol) that your view can use in an erb tag

例如

class HomeController < ApplicationController
  def index
    @some_records = SomeModel.all
  end
end

然后可以像这样在index.html.erb文件中使用

This can then be used in the index.html.erb file like so

<ul>
  <% @some_records.each do |rec| %>
    <li> A record: <%=rec.some_method%> </li>
  <% end %>
</ul>

因为有一条路由,所以可以将路由名称用作链接和按钮的路径,以将数据从浏览器发布回服务器,从而整个过程重新开始.

Because you have a route you can use the routes name as a path for links and buttons to post data from the browser back to the server where the whole process starts all over again.

这并不是对所有组件如何确切在一起的严格描述,但它应该足够紧密以使您了解所有组件的发生方式

That's not a strict description of exactly how it all hangs together but it shou close enough to give you an idea of how it all happens

这篇关于如何连接模型,视图和控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆