“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架構(gòu)
Model是我的應(yīng)用程序的數(shù)據(jù)以及處理這些數(shù)據(jù)的規(guī)則。在Rails中,model主要用來管理數(shù)據(jù)庫(kù)中表的interaction。多數(shù)情況下,一張數(shù)據(jù)庫(kù)中的table可以對(duì)應(yīng)一個(gè)model。“The bulk of your application’s business logic will be concentrated in the models.”
View就是你的應(yīng)用程序的UI。在Rails中,views一般都是html的文件(with embedded Ruby code that performs tasks related solely to the presentation of the data). Views可以用來為你的項(xiàng)目提供數(shù)據(jù),或者提供其他工具來幫你的項(xiàng)目發(fā)送請(qǐng)求。
Active Record: “負(fù)責(zé)創(chuàng)建和使用需要持久存入數(shù)據(jù)庫(kù)中的數(shù)據(jù)。Active Record 實(shí)現(xiàn)了 Active Record 模式,是一種對(duì)象關(guān)系映射系統(tǒng)。Active Record 模式:對(duì)象中既有持久存儲(chǔ)的數(shù)據(jù),也有針對(duì)數(shù)據(jù)的操作。Active Record 模式把數(shù)據(jù)存取邏輯作為對(duì)象的一部分,處理對(duì)象的用戶知道如何把數(shù)據(jù)寫入數(shù)據(jù)庫(kù),還知道如何從數(shù)據(jù)庫(kù)中讀出數(shù)據(jù)。”
Active Resource:提供商業(yè)對(duì)象和RESTful web服務(wù)之間的鏈接,“It implements a way to map web-based resources to local objects with CRUD semantics.”
Active Support::“為了減輕應(yīng)用的負(fù)擔(dān),默認(rèn)情況下 Active Support 不會(huì)加載任何功能。Active Support 中的各部分功能是相對(duì)獨(dú)立的,可以只加載需要的功能,也可以方便地加載相互聯(lián)系的功能,或者加載全部功能。”
Rails項(xiàng)目結(jié)構(gòu)分析
README:使用須知。
Rakefile:包含了可以在終端運(yùn)行的batch jobs。
app:包含了上文提到的MVC。
config:項(xiàng)目的runtime rules, routes, databases, and more。
db:數(shù)據(jù)庫(kù)的schema 和 migrations。
doc:項(xiàng)目具體的文檔
lib:Extended modules for your application
log: Application log files.
public: 放你的圖片,html, css, JS 以及其他任何靜態(tài)文件。
script:do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
注意事項(xiàng): 基本每個(gè)Rails程序都會(huì)跟數(shù)據(jù)庫(kù)打交道,要用的數(shù)據(jù)庫(kù)要在config/database.yml文件中聲明。如果你打開一個(gè)新的rails項(xiàng)目的config/database.yml文件,你會(huì)發(fā)現(xiàn)Default數(shù)據(jù)庫(kù)是SQLite。而且會(huì)有三個(gè)不同的環(huán)境: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.”