elz: (ancestor)
[personal profile] elz posting in [community profile] rubyonrails
In a Rails application, most of your code is divided into three main sections: the models, the controllers, and the views. Which code goes where is one of those things that can be a little confusing when you're getting started with Rails.



Models



Let's say I'm writing a blog application - I want to create posts that other people can read. If I generate a scaffold for that, I'll wind up with a file named post.rb in the models folder (in the app folder). The first line will say:

class Post < ActiveRecord::Base

Ignore the "< ActiveRecord::Base" part for now - all that means is that your class inherits from a class that Rails gives you which has a bunch of useful methods. What you should focus on is that this is *the* post class - this is where you'd put any code that has to do with what a post is, what a post does, what should happen to a post before it gets saved or deleted, which posts to get from the database, what sort of relationships a post has with other objects (users, comments, etc.). The models are the real heart of your application, and any code that can be moved out of the controller or the view and into the model probably should be.

Note: one thing "< ActiveRecord::Base" does is draw a connection between your Ruby class and a database table so that an instance of the Post class is a record from the posts table. (This is called Object-relational mapping or ORM.) Model classes don't have to inherit from ActiveRecord::Base, though, and they don't have to be connected to database tables - you can put any sort of Ruby class in there if you need to. It doesn't come up as often, but you can do it.

Controllers



Continuing with the blog theme, at the top of your posts controller, it would say:

class PostsController < ApplicationController

This is a different kind of class that inherits a different set of methods. The controller is a sort of gatekeeper - it gets information from the model and sends it to the view, and it gets information back from the view and sends it to the model. Generally speaking, the less code you have in your controller, the better off you are. I've seen tutorials that say something like "models should be smart, controllers should be dumb" but I think it's more like the controller should be concise because it's where things actually happen - it's where you create new records, update them, delete them, display them, and the less clutter there is, the easier it is to keep track of what's actually going on. Also, things like complex database finds (get all public posts by a certain user, tagged with a certain tag, in a certain order, etc.) tend to be easier to maintain and less likely to be duplicated if the bulk of the code is in the model.

What should go in the controller? Most basically:

1. Assignments of instance variables that get passed to the view (@post = Post.new, @post = Post.find(params[:id]), @posts = Post.all)

2. Calls to methods that make changes to the database in response to user input (@post.save, @post.destroy)

3. Code that controls who can see which pages

4. Flash messages and redirects

Views



Views are probably the easiest to grasp when you're starting out because they're the most like regular webpages. They are html once the Ruby code in <%= %> brackets is evaluated, and the goal is generally to include as little Ruby as possible so that you can see and focus on how the page is structured and displayed. Any information you need from the database should generally be assigned to an instance variable in the controller and not accessed directly from the view.

When you find yourself using if/else or multiple lines of Ruby in your view, that's where a helper method is generally useful. If you've created any scaffolds in your application, you'll notice that the app folder contains four other folders: controllers, helpers, models and views, and yet the system is called Model-View-Controller, not Model-View-Helper-Controller. Helpers are just an extension of the views - they're a way to simplify code and make it easier to reuse. For example, let's say there's a spot on my page where I want to have a login link if you're not logged in and a log out link if you are - instead of having five lines of Ruby cluttering up my page - and repeated wherever I want that link - I can make a helper method called login_logout_link (or something). I'd define it in a helper file and just put <%= login_logout_link %> in my view.

When you're just starting out, the most important thing to know about helpers is that they exist, and if you see a mystery method in the middle of your view and you don't know what the heck it is, it's probably a helper. :)

(no subject)

Date: 2009-05-20 12:22 am (UTC)
From: [personal profile] ex_tal644
This is awesome!

kTIDPJLmYe

Date: 2013-05-26 09:47 am (UTC)
From: (Anonymous)
What spectacular tiinmg for this thread! I love everything posted so far!I'm finishing up my BFA in Studio Arts with a concentration in Drawing. Looking to get my MFA in Painting. I love ball point pens, watercolor, and oil paint. has several of my pieces. But I thought these would be of particular interest. These are pieces from my Star Stuff series (and they just got finished last week.) They're watercolor, ball point pen & ink.

loBNaDyczltqMXAFgW

Date: 2013-05-26 05:29 am (UTC)
From: (Anonymous)
I use that trick for other things, but I have 30,000+ conpimaes in my database. The include trick is not efficient. I keep the conpimaes in a cache.Mostly, my point is to be aware of all the magic that Ruby is doing. The easy stuff works great when you start off, but you will want to watch the database calls.

vKBmbtiTowrWWPMcg

Date: 2013-05-26 02:29 pm (UTC)
From: (Anonymous)
My daughter used to know ahntoer child with this condition aged 10.She has had some difficult times and when symptoms flare was put on a special milk diet by her hospital for things to calm and then she can resume eating again.She is able to lead a mostly normal life inbetween so i guess its down to finding what will work well for your son as soon as possible.Im sorry i cant offer any more ideas but as each person can display varying degrees of this illness, your safer bet would be to keep getting refered by your doctor until you and your son feel the Crohns is under some control.Try to see if this website can help direct you bothVery best of luck to you both x

KJAZZQFrPUpiPzBxzmM

Date: 2013-05-26 07:57 pm (UTC)
From: (Anonymous)
只要將 Query 盡量善用 Rails 3 的 AREL 的架構去下, 就可以做到 lazy load. (就是 view 呼叫 each 之類的才會真的 query db).如此拉資料的邏輯還是在 Controller 裡. View 也可以一如往常再加 fragment cache. 如此就仍能保有一定的效率 (雖然還是在吃 cache 前產生了 AREL 的物件計算, 不過那應該影響非常小), 同時還能 keep the rule, 維持一致性的結構啦.

MEPiLUjlYrmQkm

Date: 2013-06-11 11:30 am (UTC)
From: (Anonymous)
I believe that stfrwaoe is improving exponentially. A developer/designer can do more each year for the same amount of typing.Furthermore, my own experiments with speech recognition & dialog suggest that programmers will be able to interact with their development environments with voice input.I work primarily as a sole developer and solving programming problems has become much easier in recent years with the knowledge repositories provided by Internet sites such as Stack Overflow.

July 2010

S M T W T F S
    123
4567 8910
11121314151617
18192021222324
25262728293031

Page Summary

Style Credit

Expand Cut Tags

No cut tags