Can someone explain RoR MVC from a C++ or Java perspective?

is a model similiar to a class? and active record is similiar to how
Java classes inherit from Object?

View is nothing more than an output of a model with HTML?

Controller is basically the Main part of the program?

Yes, model is just a class. It is the domain object. Active record gives
you the ORM functionality
to your model classes. You just extend your model class from
ActiveRecord class.

View is similar to jsp pages. It has a template that can be .rhtml with
ruby code embedded in
html. Think of Controller class as the Action class in Struts. Welcome
to the wonderful
wonderland!!!

bbqDude wrote:

is a model similiar to a class? and active record is similiar to how
Java classes inherit from Object?

View is nothing more than an output of a model with HTML?

Controller is basically the Main part of the program?

Controllers and models are all Classes, and every instance of any object
inherits form class Object in Ruby.

Controller:
Handles requests and glues Models and Views together. When a request
comes in the controller has two responsibilities. Change the state of
models, if necesary, and retrieve data that the view will need.
“actions” each have a unique url, and each action is a public method of
a particular controller object. So for controller People and the urls
“/people”, “people/show/1”, “people/edit/1” the controller would have
the public methods index, show, and edit.

The more code that can be put in the model, rather than the controller,
the better. For example:

#Bad:
def charge_order
order = Order.find(params[:id])
result = CreditAPI.charge(1234123412341234, 9.99)
if result.success?
order.staus = :charged
order.save
end
end

#Good:
def charge_order
Order.find(params[:id])
order.charge!(1234123412341234, 9.99)
end

Model:
Wraps a particular database table, each column in the database gets an
attribute reader/writer so you can do “@post.title = ‘foo’” and whatnot.
A model may also be a Class that does not inherit form ActiveRecord,
such as a ShoppingCart which simply exists in the session until an order
is saved.

View:
Renders HTML (or RJS, or XML, etc.) based on the instance variable
defined in the controller action that processed the request. For
instance, in the controller:

class BlogController < ApplicationController
def index
@posts = Post.find(:all)
end
end

And say you wanted a list of the titles of all the posts. You would
have a view files called /app/views/blog/index.rhtml with the following:

    <% for post in @posts %>
  • <%= post.title %>
  • <% end %>

Hello Alex,
Thanks for an awesome explanation! I was just having trouble on what
belongs in a model vs in a controller. Thanks for putting things in
perspective!

you said put as much as you can in the model, why is this? and where is
the
demarkation in your controller actions that should be split into the
model
and controller, i.e what is the rule of thumb for putting actions in the
model?

#Bad:
def charge_order
order = Order.find(params[:id])
result = CreditAPI.charge(1234123412341234, 9.99)
if result.success?
order.staus = :charged
order.save
end
end

#Good:
def charge_order
Order.find(params[:id])
order.charge!(1234123412341234, 9.99)
end

On 7/31/06, bbqDude [email protected] wrote:

Hello Alex,
Thanks for an awesome explanation! I was just having trouble on what
belongs in a model vs in a controller. Thanks for putting things in
perspective!

It would probably help to read up about the MVC design pattern in
either of the books /Head First Design Patterns/ or /Design Patterns/.
First explains the pattern in Java, and the other one explains the
pattern in C++ (I think).

Joe