
本文與本系列之前的文章非常相似,但這次我們將使用Tailwind框架作為無類CSS框架。
創(chuàng)建新的Rails應(yīng)用
-
rails serve
命令前的 time
用于顯示命令執(zhí)行的總時間。以下示例耗時47秒。
<code>$ rails -v
Rails 8.0.0
$ time rails new classless-css-tailwind
...
real 0m47.500s
user 0m33.052s
sys 0m4.249s</code>
Rails 8 基于其“No Build”理念,默認(rèn)使用Propshaft作為資源管道庫和Importmap作為JavaScript庫。Importmap不執(zhí)行任何JavaScript處理。
使用VSCode或您喜歡的編輯器打開項目
<code>$ rails -v
Rails 8.0.0
$ time rails new classless-css-tailwind
...
real 0m47.500s
user 0m33.052s
sys 0m4.249s</code>
了解Rails默認(rèn)布局 app/views/layouts/application.html.erb
展開…
- 遵循約定優(yōu)于配置(CoC)原則,Rails使用`application.html.erb`作為默認(rèn)布局來渲染所有頁面;
- Rails 8中的原始文件內(nèi)容應(yīng)與以下內(nèi)容相同或相似:
<code>$ cd classless-css-tailwind && code .</code>
-
<head>
標(biāo)簽內(nèi)包含頁面渲染和正常運行的重要結(jié)構(gòu)元素。<head>
用于包含元數(shù)據(jù)和重要資源,這些資源有助于配置頁面的行為(使用JavaScript)、外觀(使用CSS)、與其他系統(tǒng)和服務(wù)的關(guān)聯(lián)以及安全設(shè)置,例如CSRF和CSP保護(hù);
- 頁面的主要內(nèi)容將通過ERB標(biāo)簽在
<body>
內(nèi)渲染。此標(biāo)簽作為集成點,用于包含Rails動態(tài)渲染的視圖內(nèi)容;
使用pages
控制器和html_test_1
、html_test_2
、html_test_3
和html_test_4
操作生成測試頁面
展開…
```bash
$ rails g controller pages html_test_1 html_test_2 html_test_3 html_test_4
create app/controllers/pages_controller.rb
route get "pages/html_test_1"
get "pages/html_test_2"
get "pages/html_test_3"
get "pages/html_test_4"
invoke erb
create app/views/pages
create app/views/pages/html_test_1.html.erb
create app/views/pages/html_test_2.html.erb
create app/views/pages/html_test_3.html.erb
create app/views/pages/html_test_4.html.erb
invoke helper
create app/helpers/pages_helper.rb
```
- 由于在創(chuàng)建控制器和操作時也添加了路由,因此可以通過以下鏈接訪問任何創(chuàng)建的操作:
localhost:3000/pages/html_test_1
localhost:3000/pages/html_test_2
localhost:3000/pages/html_test_3
localhost:3000/pages/html_test_4
使用VSCode打開config/routes.rb
文件
- 在文件末尾添加以下行,將頁面根目錄定向到之前創(chuàng)建的
pages
控制器和html_test_1
操作。這樣,訪問您的網(wǎng)站或系統(tǒng)時,首先顯示的頁面將是pages
控制器的html_test_1
頁面。否則,將顯示Rails的默認(rèn)頁面。
<title></title><meta content="width=device-width,initial-scale=1" name="viewport"></meta><meta content="yes" name="apple-mobile-web-app-capable"></meta><meta content="yes" name="mobile-web-app-capable"></meta><link href="/icon.png" rel="icon" type="image/png"></link><link href="/icon.svg" rel="icon" type="image/svg+xml"></link><link href="/icon.png" rel="apple-touch-icon"></link>
- 如果在創(chuàng)建控制器時傳遞了
--skip-routes
參數(shù),則可以忽略為創(chuàng)建的操作添加路由。完整的命令將變?yōu)?code>rails g controller pages html_test_1 html_test_2 html_test_3 html_test_4 --skip-routes
顯示Rails路由
展開…
使用終端,您可以指定控制器(使用`-c`)來顯示路由,例如`pages`控制器:
<code>$ rails -v
Rails 8.0.0
$ time rails new classless-css-tailwind
...
real 0m47.500s
user 0m33.052s
sys 0m4.249s</code>
或者,您可以使用以下命令顯示所有路由:
<code>$ cd classless-css-tailwind && code .</code>
- 您還可以使用地址
http://127.0.0.1:3000/rails/info/routes
通過瀏覽器訪問路由。請勿忘記使用項目根目錄中的bin/dev
或標(biāo)準(zhǔn)rails server
啟動開發(fā)服務(wù)器。開發(fā)服務(wù)器會“監(jiān)聽”JavaScript文件和CSS文件的更改,以執(zhí)行必要的處理以將其提供給用戶。
- 為了使這些文件的更改能夠立即在瀏覽器中執(zhí)行和查看,需要安裝像Rails Live Reload這樣的gem。
我們將創(chuàng)建四個包含HTML內(nèi)容的頁面來測試CSS樣式。
Ruby on Rails默認(rèn)使用MVC(模型-視圖-控制器)架構(gòu)來啟動項目的組織。大部分代碼都組織在以下文件夾中:
- 與領(lǐng)域/業(yè)務(wù)邏輯和數(shù)據(jù)相關(guān)的代碼應(yīng)保存在
app/models
文件夾中;
- 與顯示相關(guān)的代碼(HTML、JSON、XML等)將位于
app/views
文件夾中;
- 與請求生命周期相關(guān)的代碼將位于
app/controllers
文件夾中;
插入html_test_1
頁面的內(nèi)容
展開…
- 訪問鏈接https://github.com/dbohdan/classless-css/blob/master/screenshot-page.html并復(fù)制``標(biāo)簽內(nèi)的所有內(nèi)容,如下所示
<code>$ rails -v
Rails 8.0.0
$ time rails new classless-css-tailwind
...
real 0m47.500s
user 0m33.052s
sys 0m4.249s</code>
插入html_test_2
頁面的內(nèi)容
展開…
- 訪問鏈接https://gist.github.com/tommaitland/5865229并復(fù)制`
插入html_test_3
頁面的內(nèi)容
展開…
訪問鏈接https://github.com/cbracco/html5-test-page/blob/master/index.html并復(fù)制``標(biāo)簽后的所有內(nèi)容,其中包含文本`
`
<title></title><meta content="width=device-width,initial-scale=1" name="viewport"></meta><meta content="yes" name="apple-mobile-web-app-capable"></meta><meta content="yes" name="mobile-web-app-capable"></meta><link href="/icon.png" rel="icon" type="image/png"></link><link href="/icon.svg" rel="icon" type="image/svg+xml"></link><link href="/icon.png" rel="apple-touch-icon"></link>
以上是使用 Tailwind 作為無類 CSS 框架的快速 Ruby on Rails 前端的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!
本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn