🏗️ Part 3 — MVC in Rails: Models, Views, Controllers Explained
Rails uses the MVC architecture:
- Model: Handles data and business logic (ActiveRecord)
- View: Template files (ERB or HAML) rendered to HTML
- Controller: The glue, handling requests and sending responses
Example controller:
class PostsController < ApplicationController
def index
@posts = Post.all
end
end
And the view:
<% @posts.each do |post| %>
<h2><%= post.title %></h2>
<% end %>