“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.”