bbqDude
2006-Jul-31 18:25 UTC
[Rails] 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? -- Posted via http://www.ruby-forum.com/.
Bala Paranj
2006-Jul-31 18:41 UTC
[Rails] can someone explain RoR MVC from a C++ or Java perspective?
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 <none@none.com> 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Alex Wayne
2006-Jul-31 18:47 UTC
[Rails] Re: can someone explain RoR MVC from a C++ or Java perspecti
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: <ul> <% for post in @posts %> <li><%= post.title %></li> <% end %> </ul> -- Posted via http://www.ruby-forum.com/.
bbqDude
2006-Jul-31 18:55 UTC
[Rails] Re: can someone explain RoR MVC from a C++ or Java perspecti
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! -- Posted via http://www.ruby-forum.com/.
Dion Hewson
2006-Jul-31 23:42 UTC
[Rails] Re: can someone explain RoR MVC from a C++ or Java perspecti
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 8/1/06, Alex Wayne <rubyonrails@beautifulpixel.com> wrote:> > 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: > > <ul> > <% for post in @posts %> > <li><%= post.title %></li> > <% end %> > </ul> > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060731/6cff327c/attachment-0001.html
Joe Van Dyk
2006-Jul-31 23:55 UTC
[Rails] Re: can someone explain RoR MVC from a C++ or Java perspecti
On 7/31/06, bbqDude <none@none.com> 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