Newbie: Models and Views and Controllers, oh my

Hi!

I am very new to Ruby on Rails; I just started this week and I'm

struggling to try to understand it. I’ve read the LAMP tutorial along
with
Amy’s, and started reading the Agile Web D. book.

I am having some problems though.  First off, I am very (still) 

confused
about Models, Views, and Controllers. From my understanding, views are
what
the end user sees. Models do database stuff and Controllers is the
space
between Views and Models. Basically, it tells the database to import
data,
check/validate data, etc. Am I on the right path so far?

When creating a Rails app, you run 'ruby script/generate 

'.
The something being either model, controller, or scaffold. How do you
know
which one to run? Is there one that just creates all three (model,
view,
and controller)?

 I'm sure I'm going to have a bunch more questions later, but I just

need a little kick to start going first.

Thanks!
Mike


View my blog!
http://wiz561.blogspot.com/

The next step we recommend to everyone is to read the book “Agile
Development with Ruby on Rails”. It will answer all of your questions.

models : represent data / control access to data (create read update
delete)
/ validate data / contain all business logic

controllers: route user requests to models

views: the user screens.

A User model has methods that let me find all users in the system.

A User controller has a method that allows a user to request a list of
users
by calling this URL:

user/list

This controller invokes the method on the User model to retrieve the
users into a variable.

A list view (located in views/user/list.rhtml) then contains the code
that
displays the results by using the variable set in the controller.

That’s pretty much all there is to models, views, and controllers… but
read the book. It’s valuable to newcomers.

Definitely read the book. Twice. =)

if you create a table in a database, set up config/database.yml with
your DB info, and do
ruby script/generate scaffold [table name]
it will generate your model, controller and several views(for CRUD
functions)
the first time I tried that while learning rails and going through
AWDWR, it blew my mind. =)

Jason

What is AWDWR?

I picked up the Agile Development w/ Ruby R. book and started going
through it. But, I have to agree with you, maybe I should read it
twice…or three times.

I feel like there’s a LOT of power using Ruby on Rails and that is why
I really want to learn it. But at the same time, it’s hard giving up
PHP because that’s what I know.

Looks like I have some reading to do this weekend!!!

Thanks for your help,
Mike

Welcome to Rails!!!

About Models, Views, and Controllers…

These concepts are things that make up the MVC architectural pattern.
The benefit of doing MVC is that it helps separate logic and
responsibility in applications… Compare the architecture shown in this
blog posting on application layering by Keith Ray -
http://homepage.mac.com/keithray/blog/2006/09/22/ - and think of the
model view and controller as being different layers (View sits on top of
controller sits on top of model)…

The goal in MVC is for models to be completely non-dependent from views,
and making it possible to have different views against the same model.

Yes, views are essentially what the user sees…

The best way to understand the interaction, and responsibilities is to
consider what happens when a user makes a request… Let’s say you’ve
done scaffolding properly on a User, you could implement a User_Admin
controller, and to edit an existing User entry, you would follow a link
like this

http://localhost:3000/user_admin/edit/1

(let’s assume you’ve done all of the following basic steps which you
generally need to do when creating any new rails app:

  1. rails
  2. edit config\database.yml
  3. ruby script\generate model
  4. edit auto-generated migration in db\migration
  5. run ‘rake migrate’
  6. create controllers using a syntax like:
    ruby script\generate controller MyController index find
    7 create some views
  7. run ruby script\server to run your web app locally
  8. browse http://localhost:3000/<controller_name> to see your app
    running

created a basic User model, updated the migration to create a table with
the right columns, run the migration, then created some basic
scaffolding, run WEBrick locally

What rails when the user makes the request is look for a controller with
the name of ‘user_admin’, then within this controller, the action
‘edit’.

Let’s say you had the User_Admin_Controller controller class with the
action edit, who’s job is to retrieve a user to be edited, you’d
probably want code that looks like this (scaffolding writes this for
you)

class User_Admin_Controller < ApplicationController
def edit
@user = User.find(params[:id]) # retrieve the user with the given ID
from the model
end
end

So in the controller, we use the User ActiveRecord to get a User object.
Note that models can be plain old classes, they don’t have to be
ActiveRecord classes. The advantage of an ActiveRecord class in Rails
is that it is very easy to save their state to a database.

After the controller action is invoked, rails then tries to find a view
with the same name as the action (in this case, it would look in
/app/views/User_Admin/edit.rhtml)

When you initialize instance variables in controllers, like @user = …
or @welcome_string = “hello there”, in views you can display those
values using the notation <%= @user %> or <%= welcome_string %>… The
view’s job then becomes about displaying the appropriate data obtained
from the controller, and to also make it possible for the user to
further interact with your application (more links, buttons, etc)

Hope this helps!!!

Dominique

Michael Wisniewski wrote:

Hi!

I am very new to Ruby on Rails; I just started this week and I'm

struggling to try to understand it. I’ve read the LAMP tutorial along
with
Amy’s, and started reading the Agile Web D. book.

I am having some problems though.  First off, I am very (still) 

confused
about Models, Views, and Controllers. From my understanding, views are
what
the end user sees. Models do database stuff and Controllers is the
space
between Views and Models. Basically, it tells the database to import
data,
check/validate data, etc. Am I on the right path so far?

When creating a Rails app, you run 'ruby script/generate 

'.
The something being either model, controller, or scaffold. How do you
know
which one to run? Is there one that just creates all three (model,
view,
and controller)?

 I'm sure I'm going to have a bunch more questions later, but I just

need a little kick to start going first.

Thanks!
Mike


View my blog!
http://wiz561.blogspot.com/

Learn Ruby first.

Programming Ruby (commonly referred to as the Pickaxe book)
1st ed.: http://www.rubycentral.com/book/ or
http://www.ruby-doc.org/docs/ProgrammingRuby/
2nd ed.: http://pragmaticprogrammer.com/titles/ruby/index.html

Then learn Rails.

Agile Web D. with Rails (commonly referred to as AWDWR)
1st ed: check your favorite local or online book store
2nd ed: http://www.pragmaticprogrammer.com/title/rails/

You’ll be a happier person.

Once you have the basics you can supplement your self-education with
blogs, wikis, forums, irc, etc. Oh and experiment.

Hi –

On Mon, 25 Sep 2006, [email protected] wrote:

learning any language, I find that they are all similar, it’s just a
matter of learning the syntax.

Now, the problem. It seems like the first book (learning Ruby) is
good, but doesn’t talk much about databases. Now the second book
(AWDwR) is good, but jumps into databases too quickly. Too bad there
isn’t something in the middle…or is there?

I’m not sure I’d describe it as being in the middle, as regards the
two Dave T. books – it’s really a different kind of take on the
subject matter – but you might get some mileage out of my book “Ruby
for Rails”. It’s essentially a presentation of Ruby, optimized for
the special focuses and concerns of the Rails developer.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Alright, I started to look at yet, another book, and I think I found
the winner.

The “Ruby on Rails: Up and Running” book from Curt H. and Bruce Tate
seems to be, at least for me, the bridge between "Pragmatic Programmers

  • Programming Ruby" and AWDwR.

I haven’t been through the whole book yet, but just going through some
of the example chapters, I thought was awesome. It explains how to get
your database configured and the relationships created. And best of
all, it all pretty much makes sense.

I think if I go through the book another time or two, it will make a
LOT more sense. If nobody has seen it, definiately pick it up. It
just came out and it’s from O’Reilly.

Thanks!

On 9/26/06, [email protected] [email protected] wrote:

of the example chapters, I thought was awesome. It explains how to get
your database configured and the relationships created. And best of
all, it all pretty much makes sense.

I think if I go through the book another time or two, it will make a
LOT more sense. If nobody has seen it, definiately pick it up. It
just came out and it’s from O’Reilly.

Thanks for the compliment, I’m really glad you are finding the book
helpful.
If you feel so inclined, it’d be nice if you could post some review
comments
on Amazon:

http://www.amazon.com/o/ASIN/0596101325/ref=s9_asin_title_1/104-0132011-7007920

Curt

Ah, this helps a little too. Thank you very much for taking the time
to write.

I read a lot of the “Pragmatic Programmers - Programming Ruby”, second
edition. After getting through most of the book this weekend, and how
I started to read the “Agile Web D. with Rails” book last
week, I’m still a little confused…book-wise.

There’s a book on how to learn ruby, which is great. But, like
learning any language, I find that they are all similar, it’s just a
matter of learning the syntax.

Now, the problem. It seems like the first book (learning Ruby) is
good, but doesn’t talk much about databases. Now the second book
(AWDwR) is good, but jumps into databases too quickly. Too bad there
isn’t something in the middle…or is there?

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs