日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ruby On Rails简介

發布時間:2024/8/1 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ruby On Rails简介 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下文字基本 都是 復制粘貼一些重點內容
詳情看文檔吧:
https://guides.rubyonrails.org/v2.3.11/getting_started.html
https://ruby-china.github.io/rails-guides/

Basics

什么是Ruby

“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可以用來為你的項目提供數據,或者提供其他工具來幫你的項目發送請求。
  • Controller控制器可以視作模型和視圖的中間人,讓模型中的數據可以在視圖中使用,把數據顯示給用戶,再把用戶提交的數據保存或更新到模型中。
  • Rails的組成

  • Action Controller: “Action Controller 是 MVC 中的 C(控制器)。路由器決定使用哪個控制器處理請求后,控制器負責解析請求,生成相應的輸出。Action Controller 會代為處理大多數底層工作,使用智能的約定,讓整個過程清晰明了。”
  • Action View:“通常,Action Controller 參與和數據庫的通信,并在需要時執行 CRUD 操作,然后由 Action View 負責編譯響應。Action View 模板使用混合了 HTML 標簽的嵌入式 Ruby 語言編寫。為了避免樣板代碼把模板弄亂,Action View 提供了許多輔助方法,用于創建表單、日期和字符串等常用組件。隨著開發的深入,為應用添加新的輔助方法也很容易。”
  • 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.
  • test:單元測試
  • tmp:臨時文件
  • vendor:存放第三方代碼。在ruby中,主要包含Ruby Gems和Rails源代碼(如果你有下載的話),以及其他插件。
  • Rails 范例程序

    去Documentation看吧,不搬運了🙅?♀?

    注意事項:
    基本每個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.)。

    關于UI

    UI 文檔: https://guides.rubyonrails.org/v2.3.11/layouts_and_rendering.html#asset-tags

    建立HTTP回應:

  • 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.”
  • 未完待續

    總結

    以上是生活随笔為你收集整理的Ruby On Rails简介的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。