Intermediate Rails: Understanding Models, Views and Controllers

[…] This is quite a concise overview of MVC. I highly recommend that you read up on this topic, as it is the key to cracking Rails. This article from betterexplained.com is a great resource for learning MVC in Rails. However, the best way to learn is, install the framework and start playing with it. […]

[…] This is quite a concise overview of MVC. I highly recommend that you read up on this topic, as it is the key to cracking Rails. This article from betterexplained.com is a great resource for learning MVC in Rails. However, the best way to learn is, install the framework and start playing with it. […]

I new to rails. Try to figure out how away for my application that allow users to upload their photos. The application is store photos in the database?

A small clarification: actually the guys designing Rails made a mistake. The design pattern in the diagram is not MVC, but MVP (Model-View-Presenter), a derivative of MVC. In real MVC, views are supposed to communicate directly with models, and the controller only selects what model and what view to use. This does not happen neither in RoR nor in 99% of web frameworks that cloned the same mistake. And most of all, templates != views; database != models. They should lie under them.

Thank you so very very much for this article. I’ve been beating my head against a wall for the last three months trying to find info that reads more clearly than gibberease (meaning that it’s supposed to be educational…as long as you’ve been doing web design since the beginning and are completely familiar…no, intimate with all the principles that lead to RoR) and this is one certain gem amongst the rubble. I’ve only gotten thru ~1/4 of the doc so far, but I can understand the way you think.

Here I’m trying to learn the Rails way, but I also have daily work I have to get done to keep our fledgling site running and resolving outstanding issues the original dev left undone…I just wish I had found this months ago. Almost as much as I wish I had gotten a ream of paper for my printer today while I was out so I could print a copy right now… :wink: so, thank you thank you thank you. This is going to help so much!

@Andrew: Awesome, really glad it helped! Exactly, so many articles make gigantic assumptions about what you already know, it can be hard to see it from the beginning. I’m thrilled this was able to help :).

@kalid: in a belongs_to association, what’s the difference between :model and :class_name? In you example, you have

belongs_to :primary_status, :model => ‘Status’, :foreign_key => ‘primary_status_id’

but I can’t find :model documented (I’m probably not looking in the right place). I’ve used :class_name when I need two references to the same table through a foreign key. Is that the same?

(Great article, btw…)

  • ff

@fearless: Whoops, looking at it again, yes, it should be :class_name… :model may have been for a much older version.

[…] Better Explained | learn right, not rote # Site tweaks: Faster Javascript load time (examples), HTTP caching, gzip compression # Ruby on Rails: Starting Ruby on Rails, Understanding MVC # Tools: version control, distributed VCS # Web Development: Debugging with Firefox, Javascript Reference, load XML/JSON data, gmail XSRF attack, GUIDs, making bookmarklets (tags: rails rubyonrails tutorial programming development howto) […]

[…] (as expressed here by rmurphey as it pertains to deficient use of jQuery (I’m guilty!)), and I understand MVC (model-view-controller) applications and personally value them as a superior way of coding, even though, once again, I’m guilty of […]

Be careful with the method override:

def length=(minutes)
self[:length] = minutes * 60
end

This will multiply a string by 60 when used in a form, not an integer. Better to cast this:

Be careful with the method override:

def length=(minutes)
self[:length] = minutes.to_i * 60
end

@Simon: Thanks for the tip, good catch.

Great Article. I just started with RoR
Thanks

[…] Can any one tell me how to use source code to compile and make it work on windows? | winscommunity Intermediate Rails: Understanding Models, Views and Controllers | BetterExplained The KDE Network Configuration tool on FreeBSD « Rhyous's 127.0.0.1 or ::1 Rich Morin :: […]

[…] found this site breaks it up a bit more, however they do their code in Ruby so it may confuse you. Intermediate Rails: Understanding Models, Views and Controllers | BetterExplained If all else fails, you still have wikipedia<3 Model?view?controller - Wikipedia, the free […]

Thanks, I’m still can’t get is a graphics render a view or a model?

[…] View Controller design pattern (BetterExplained has a good introduction to MVC): MVC emphasizes the separation of the business logic from the presentation logic, from the data […]

You definitely know how to teach “something”.

You can make any concept into pieces and can teach that really well.

Thanks for this great article.

@Pete: Thanks!

Holy Cow, this article is the best! I looked everywhere for an explanation to the MVC model and after reading this article, I feel like i can teach a class on how to explain it! Awesome, thank you so much for sharing!