ruby on rails 框架笔记

来源:互联网 发布:淘宝技巧 知乎 编辑:程序博客网 时间:2024/05/22 13:13

Ruby On Rails框架介绍

下文都是对上面这个文章的一个感想.

Ruby on Rails, often shortened to Rails, is an open source full-stack web application framework for the Ruby programming language. Ruby on Rails is not to be confused with Ruby, which is a general-purpose programming language, on which Ruby on Rails runs. 

Ruby on rails与ruby的关系。

Technical overview

Like many web frameworks, Ruby on Rails uses the Model/View/Controller (MVC) architecture pattern to organize application programming.

model对应数据库中的表。

controller是连接mode和view的桥梁,通过在model进行数据操作或者是相应浏览器请求等,将得到的数据返回给view。

view其实就是显示操作结果的页面。

Philosophy and design

Ruby on Rails is intended to emphasize Convention over Configuration (CoC), and the rapid development principle of Don't Repeat Yourself (DRY).

"Convention over Configuration" means a developer only needs to specify unconventional aspects of the application. For example, if there is a class Sale in the model, the corresponding table in the database is called sales by default. It is only if one deviates from this convention, such as calling the table "products sold", that the developer needs to write code regarding these names. Generally, Ruby on Rails conventions lead to less code and less repetition

 约定胜于配置,程序员在程序中自己定义一些规定,来使得整个程序的逻辑更加清晰

Motivation

Some frameworks need multiple configuration files, each with many settings. These provide information specific to each project, ranging from URLs to mappings between classes and database tables. A large number of configuration files with lots of parameters is often an indicator of an unnecessarily complex application design.[1]

For example, early versions of the well-known Java persistence mapper Hibernate mapped entities and their fields to the database by describing these relationships in XML files. Most of this information could have been revealed by conventionally mapping class names to the identically named database tables and the fields to its columns, respectively. Later versions did away with the XML configuration file and instead employed these very conventions, deviations from which can be indicated through the use of Java annotations (see JavaBeans specification, linked below).

上面是之所以提出使用CoC的动机,主要就是精简设计,减少繁琐的配置,达到敏捷开发的目的。

"Don't repeat yourself" means that information is located in a single, unambiguous place. For example, using the ActiveRecord module of Rails, the developer does not need to specify database column names in class definitions. Instead, Ruby on Rails can retrieve this information from the database based on the class name.

"Fat models, skinny controllers" means that most of the application logic should be placed within the model while leaving the controller as light as possible.

把尽量多的经常使用的功能封装在model中,在程序中尽可能少写重复的代码,把尽可能多的逻辑操作放在model中,让controller尽可能简单。


kinfey让我们看rails框架的目的是想我们把这个框架的思想领悟下来,但是由于我并没有学过j2ee也没什么web开发经验。但是,mvc的模式在不管是web开发也好,移动应用开发也好都是十分重要的,最基本的一种建模框架。

把在程序中需要重复使用的操作放在model中,controller只需要通过接口调用model中相应的方法。使得controller中的逻辑可以尽可能简单,更专注于如何于view打交道。

至于CoC的思想,这个我还没想好怎么运用,待我经验多多了,可能就知道了,继续向kinfey学习咯。

原创粉丝点击