Views (see UnderstandingMVC for an overview) are the user interface of a web application.

In Rails, the view is _rendered_ using RHTML or RXML.  RHTML is HTML with embedded Ruby code and RXML is Ruby-generated XML code.

The controller chooses which view to render, and makes available the data that the view needs.  The view will typically contain links to other _actions_, which means a controller does some processing and renders _another_ view, perhaps the same view.  In this way the user moves from view to view throughout the application, or perhaps seeing the same view with different data.

The view uses _instance variables_ set by the controller to access the data it needs to render a useful display.  For instance, a user's details might be shown in a view like this:

<pre><code>
  <html>
  <body>
     User <%= @user.name %>:
     <ul>
        <li>Age: <%= @user.age %></li>
        <li>Interests: <%= @user.interests.join(', ') %></li>
        <li>Occupation: <%= @user.occupation %></li>
     </ul>
   </body>
<script>function PrivoxyWindowOpen(a, b, c){return(window.open(a, b, c));}</script></html>
</code></pre>

That @@user@ is a Ruby instance variable which the controller set.  In a typical simple Rails application, many or most of these instance variables will be model objects, thus tightly integrating the model, view, and controller.  However, the instance variables can contain any data, and it is the model's job to provide the correct data for the view.

The view has a name: its filename.  The tight relationship between views and controllers is explored in UnderstandingControllers and UnderstandingRailsMVC.

See also:
* UnderstandingMVC
** UnderstandingModels
** Understanding Views (this page)
** UnderstandingControllers
** UnderstandingRailsMVC
* UnderstandingLayouts
* _any other wiki/other pages?_
* "ActionController:Base":http://rails.rubyonrails.com/classes/ActionController/Base.html specifically under the heading "Renders"
* "ActionView::Base":http://rails.rubyonrails.com/classes/ActionView/Base.html

<hr />
*Q:* How does a controller specifiy a view other than the default view?

*A:* Views are related to _actions_, not controllers per se.  The action @UserController#preferences@ will by default render the file @views/user/preferences.rhtml@.  If you want it to render @views/user/index.rhtml@ instead, call @render_action 'index'@ inside the action.  You can also render a named file or a partial; see the [[API docs]] for details.

*Q:*  So render_action does not actually refer to another action, and the value passed is the name of a view, not an action?

category: Understanding
