“Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several assumptions about what every developer needs to get started.”
MVC架构
Model是我的应用程序的数据以及处理这些数据的规则。在Rails中,model主要用来管理数据库中表的interaction。多数情况下,一张数据库中的table可以对应一个model。“The bulk of your application’s business logic will be concentrated in the models.”
View就是你的应用程序的UI。在Rails中,views一般都是html的文件(with embedded Ruby code that performs tasks related solely to the presentation of the data). Views可以用来为你的项目提供数据,或者提供其他工具来帮你的项目发送请求。
Active Record: “负责创建和使用需要持久存入数据库中的数据。Active Record 实现了 Active Record 模式,是一种对象关系映射系统。Active Record 模式:对象中既有持久存储的数据,也有针对数据的操作。Active Record 模式把数据存取逻辑作为对象的一部分,处理对象的用户知道如何把数据写入数据库,还知道如何从数据库中读出数据。”
Action Mailer:“实现发送邮件功能,邮件由邮件程序和视图控制”
Active Resource:提供商业对象和RESTful web服务之间的链接,“It implements a way to map web-based resources to local objects with CRUD semantics.”
Active Support::“为了减轻应用的负担,默认情况下 Active Support 不会加载任何功能。Active Support 中的各部分功能是相对独立的,可以只加载需要的功能,也可以方便地加载相互联系的功能,或者加载全部功能。”
Rails项目结构分析
README:使用须知。
Rakefile:包含了可以在终端运行的batch jobs。
app:包含了上文提到的MVC。
config:项目的runtime rules, routes, databases, and more。
db:数据库的schema 和 migrations。
doc:项目具体的文档
lib:Extended modules for your application
log: Application log files.
public: 放你的图片,html, css, JS 以及其他任何静态文件。
script:do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
注意事项: 基本每个Rails程序都会跟数据库打交道,要用的数据库要在config/database.yml文件中声明。如果你打开一个新的rails项目的config/database.yml文件,你会发现Default数据库是SQLite。而且会有三个不同的环境:development (used on your development computer as you interact manually with the application),test (The test environment is used to run automated tests),production(The production environment is used when you deploy your application for the world to use.)。
Call render to create a full response to send back to the browser. “In most cases, the ActionController::Base#render method does the heavy lifting of rendering your application’s content for use by a browser. There are a variety of ways to customize the behavior of render. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.”
Call redirect_to to send an HTTP redirect status code to the browser. “it tells the browser to send a new request for a different URL”
Call head to create a response consisting solely of HTTP headers to send back to the browser. “The head method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling render :nothing. The head method takes one response, which is interpreted as a hash of header names and values.”