📜  Ruby on Rails MVC(1)

📅  最后修改于: 2023-12-03 15:04:56.773000             🧑  作者: Mango

Ruby on Rails MVC

Ruby on Rails is a web application framework written in the Ruby programming language. It follows the Model-View-Controller (MVC) architectural pattern to separate concerns and increase maintainability.

Model-View-Controller (MVC)

MVC is a design pattern that separates an application into three interconnected components: the model, view, and controller. Each component is responsible for a specific piece of functionality and all three work together to create a cohesive application.

Model

The model represents the data and business logic of the application. It handles data storage, retrieval, and modification. In Ruby on Rails, the model is represented by an ActiveRecord model.

class User < ActiveRecord::Base
  has_many :posts
  validates :email, presence: true, uniqueness: true
end
View

The view is responsible for presenting data to the user. It displays the data retrieved from the model in a user-friendly format. In Ruby on Rails, the view is represented by an HTML template.

<h1><%= @user.name %></h1>
<p><%= @user.bio %></p>
Controller

The controller acts as the intermediary between the model and the view. It receives requests from the user, retrieves data from the model, and passes that data to the view for display. In Ruby on Rails, the controller is represented by a controller class.

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end
Ruby on Rails Implementation of MVC

In Ruby on Rails, the MVC pattern is implemented through a combination of conventions and code. The framework provides default file structures and naming conventions that help developers organize their code in a way that follows the MVC pattern.

File Structure

In Ruby on Rails, the file structure is organized by component, with separate directories for the model, view, and controller.

├── app
│   ├── controllers
│   │   ├── users_controller.rb
│   ├── models
│   │   ├── user.rb
│   ├── views
│   │   ├── users
│   │   │   ├── show.html.erb
Routing

Routing in Ruby on Rails maps URLs to controller actions. The config/routes.rb file defines the available routes and maps them to the appropriate controller action.

Rails.application.routes.draw do
  resources :users, only: [:show]
end
Views

Views in Ruby on Rails use Embedded Ruby (ERB) templates to render HTML. The templates access data passed in from the controller through instance variables.

<h1><%= @user.name %></h1>
<p><%= @user.bio %></p>
Controllers

Controllers in Ruby on Rails inherit from the ApplicationController class and contain methods that correspond to the available routes. They interact with the model to retrieve data and pass it to the view for rendering.

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end
Conclusion

Ruby on Rails uses the Model-View-Controller (MVC) pattern to help developers organize their code, separate concerns, and increase maintainability. The framework provides default conventions and naming conventions that make it easy to implement the MVC pattern in a consistent and scalable way.