开发者

Ruby on Rails MVC clarification

I'm learning RoR and two different sources describe the the View's relat开发者_如何学Goionship with the browser somewhat differently. One source describes the Controller as returning requests to the browser after interacting with the View. Another source describes the View as returning requests directly to the browser after receiving instructions from the Controller. So should I understand the Controller to be between the browser and the View or does the View come between the Controller and the browser?


I think both interpretations are partly right and partly wrong since they're both gross simplifications.

When a request is passed to the controller, it executes the method that corresponds to the action that's been requested. One of the things done by that method is to render the view -- that's what the call the render does. If you don't explicitly call render, it's called for you with the default params. Neither one is really "between" the other and the browser, but the controller is capable of responding to requests on its own, and the view is not.


I'm not an RoR expert. But from a technical and architectural standpoint, we should consider the view in all standard MVC frameworks as passive. Which means that it never "does" anything - a view works just as some sort of UI template, describing how the screen (or page) should be rendered.

So in the end it will be the controller that will render the HTML page, based upon the view definition, and serve it to the end user.


The controller returns the request using the view. It's possible to respond a request without the view (by redirecting, rendering nothing, a static string, a JSON or XML - although you could argue, that the later three are view logic inside of the controller), but not to respond to a request without a controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜