DEV Community

hmza
hmza

Posted on

πŸ—οΈ Part 3 β€” MVC in Rails: Models, Views, Controllers Explained

πŸ—οΈ 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 
Enter fullscreen mode Exit fullscreen mode

And the view:

 <% @posts.each do |post| %> <h2><%= post.title %></h2> <% end %> 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)