🏗️ Part 3 — MVC in Rails: Models, Views, Controllers Explained
hmza

hmza @hmzas

About: `Git commit -m 'Hope it works'.` `404: Social life not found.`

Location:
Mars
Joined:
Aug 6, 2024

🏗️ Part 3 — MVC in Rails: Models, Views, Controllers Explained

Publish Date: Jul 15
5 0

🏗️ 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

Comments 0 total

    Add comment