<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Octaviano Chen on Medium]]></title>
        <description><![CDATA[Stories by Octaviano Chen on Medium]]></description>
        <link>https://medium.com/@ItsBoyu?source=rss-ceaec2e8730a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*dC6CHAzDgqEBwCntYpHLEA.jpeg</url>
            <title>Stories by Octaviano Chen on Medium</title>
            <link>https://medium.com/@ItsBoyu?source=rss-ceaec2e8730a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 00:03:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ItsBoyu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[如何在 Rails 專案套用 Bootstrap 樣式]]></title>
            <link>https://medium.com/its-boyu-s-note/%E5%A6%82%E4%BD%95%E5%9C%A8-rails-%E5%B0%88%E6%A1%88%E5%A5%97%E7%94%A8-bootstrap-%E6%A8%A3%E5%BC%8F-e04f48bbecfb?source=rss-ceaec2e8730a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e04f48bbecfb</guid>
            <category><![CDATA[webpacker]]></category>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Octaviano Chen]]></dc:creator>
            <pubDate>Sun, 12 Apr 2020 19:52:42 GMT</pubDate>
            <atom:updated>2020-04-12T19:58:19.538Z</atom:updated>
            <content:encoded><![CDATA[<h4>Rails 學習筆記</h4><p>在學習使用 <em>Rails</em> 框架來開發專案時，對於 <em>RMVC</em> 結構中的 <em>Views</em> 總是先以陽春的 <em>HTML</em> 標籤來進行排版，充分達到沒人想要使用的效果，因此，趕快透過套用 <em>Bootstrap</em> 的樣式來拯救一下吧！</p><p><strong>方法一：Copy &amp; Paste</strong></p><p>可以按照之前學習前端切版時的方式，到 <a href="https://getbootstrap.com/docs/4.4/getting-started/introduction/"><em>Bootstrap</em> 官方網站</a> 複製 <em>Starter template</em> 程式碼，並將整段內容貼到 <em>Rails</em> 專案中 <em>app/views </em>路徑上各個需要套用的<em> erb </em>檔案裡，若之前已經設定好 <em>Snippe</em>t 的話，處理起來就更便捷了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Furp2vGpTz7GJb3aVIp0bg.png" /><figcaption>將 Start template 程式碼貼在 index.html.erb</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T4ikRO3eXJn-JVwOBFdaFA.png" /><figcaption>套用 Bootstrap 樣式效果</figcaption></figure><p>不過透過這樣的方式來使用 <em>Bootstrap</em>，就必須要在每個套用樣式的 <em>erb</em> 檔案裡貼上重複的程式碼，有沒有什麼方法可以只貼一次就一勞永逸呢？這時候可以到 <em>app/views/layouts</em> 路徑裡找到名為 <em>application.html.erb</em> 的檔案，寫在這個檔案裡的程式碼，若沒有特別設定，將會套用至整個專案裡所有子層 <em>view</em> 的頁面。將 <em>Start template</em> 的程式碼貼上時要注意不要覆蓋掉 <em>Ruby</em> 的程式碼（ 前面四行放在 <em>&lt;head&gt;</em>標籤，最後一行放在<em>&lt;body&gt;</em>標籤 ），接著可以到 <em>index.html.erb</em> 測試能否套用樣式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6N_nPinhDHbZpE3FWS8K4g.png" /><figcaption>將 Start template 程式碼貼在 application.html.erb</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0JwbCdI1z1OEfGk9tTTNGQ.png" /><figcaption>在 index.html.erb 使用 格線 與 按鈕 樣式</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gg6-yHrrU_WXvc7kpRzMnw.png" /><figcaption>套用 Bootstrap 樣式效果</figcaption></figure><p><strong>方法二：安裝套件</strong></p><p>如果仔細觀察前面提到的 <em>application.html.erb</em> 檔案，會發現其中有兩行 <em>Ruby</em> 的程式碼寫著 <em>stylesheet_link_tag &#39;application&#39;</em>跟 <em>javascript_pack_tag &#39;application&#39;</em> ，而這兩行程式碼分別代表兩個進入點，前者會指向路徑<em>app/assets/stylesheets/application.css </em>的檔案，透過 <em>*= require_tree .</em>跟 <br><em>*= require_self</em> 程式碼可以將整個 <em>stylesheets</em> 資料夾的樣式打包成一個 <em>css</em> 檔案；後者則指向路徑為 <em>app/javascript/packs/application.js</em> 的檔案，同樣透過 <em>require</em> 的方式來將要套用的套件打包成一個 <em>js</em> 檔案。</p><p>註：由於筆者使用<em> Rails 6 </em>版本進行開發，<em>Webpacker</em> 已成標準配備，當在終端機使用 <em>$ rails new</em>指令建立新專案時，可以看到 <em>webpacker:install</em> 的訊息，因此將以 <em>JS</em> 套件安裝作為方法二的示範。</p><p>首先，在終端機輸入<em>$ yarn add bootstrap </em>指令，即可透過 <em>yarn</em> 這個套件版本管理工具來安裝 <em>bootstrap</em> 套件，完成後可在專案目錄中的 <em>package.json</em> 跟 <em>yarn.lock</em> 兩個檔案中看到 <em>&quot;bootstrap&quot;: &quot;^4.4.1&quot;</em> （ 將顯示實際安裝版本 ）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d5vrBrLSrOxyHoSpzGXDFQ.png" /><figcaption>package.json 中紀錄安裝 bootstrap 套件版本</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*03nPhDKUbhZ_7GWykqSkwA.png" /><figcaption>yarn.lock 中紀錄安裝 bootstrap 套件版本</figcaption></figure><p>雖然我們已經將套件安裝完成，不過並不會自動套用 <em>Bootstrap</em> 的樣式，必須將其載入前面提及的 <em>webpack</em> 進入點才能使用。因此請先至專案目錄裡確認 <em>node_modules/bootstrap/scss/bootstrap.scss</em> 檔案路徑，並在 <em>app/javascript</em> 目錄下新增 <em>style</em> 資料夾及 <em>index.js</em> 、<em>style.scss</em> 兩個檔案後執行以下步驟：</p><ol><li>開啟 <em>style.scss</em> 檔案輸入 <em>@import &#39;bootstrap/scss/bootstrap.scss&#39;</em></li><li>開啟 <em>index.js</em> 檔案輸入 <em>import &#39;./style.scss&#39;</em></li><li>開啟 <em>javascript/packs/application.js</em> 檔案輸入 <em>require &#39;style&#39;</em>：會自動到 <em>style</em> 目錄找 <em>index.js</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*I8Q32GQaXGFt7ehAR1OLFA.png" /><figcaption>path : app / javascript / style / style.scss</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*dzY1AWTaK9GrxSInGlZ7Vg.png" /><figcaption>path : app / javascript / style / index.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6_47iUirs5DWuOYj5a1Qsg.png" /><figcaption>path : app / javascript / packs / application.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jFcglhezmiFpfSzriUwrQQ.png" /><figcaption>套用 Bootstrap 樣式效果</figcaption></figure><p>成功套用樣式以後，如果這時候試著將 <em>Bootstrap</em> 的 <em>Dropdown</em> 程式碼貼到 <em>erb</em> 裡會發現：「咦？奇怪！怎麼不會動呢？」，原來剛剛只有套用 <em>CSS</em> 樣式，需要 <em>JS</em> 操控的部分並沒有套用到，因此接著要處理 <em>jQuery</em> 跟 <em>popper.js </em>兩個套件的安裝，在終端機輸入 <em>$ yarn add jquery popper.js</em> 指令，安裝完成後同樣可以在<em> package.json</em> 跟 <em>yarn.lock</em> 兩個檔案中看到安裝的版號，接著再到 <em>app/javascript/packs/application</em> 中依序輸入 <em>require &#39;jquery&#39;</em>、 <em>require &#39;popper.js&#39;</em>、 <em>require &#39;bootstrap&#39;</em> 即可完成 JS 的套用囉！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XBYW8ANsKH7mHKn93Q61uw.png" /><figcaption>path : app / javascript / packs / application.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iv2vd6FiiQgxEPbnjX9jYA.png" /><figcaption>套用 Bootstrap JS 效果</figcaption></figure><p>參考資料：</p><ul><li><a href="https://rossta.net/blog/webpacker-with-bootstrap.html">Using Bootstrap with Rails Webpacker</a></li><li><a href="https://getbootstrap.com/docs/4.4/getting-started/download/">Bootstrap 官方網站</a></li><li>ASTRO Camp</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e04f48bbecfb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/its-boyu-s-note/%E5%A6%82%E4%BD%95%E5%9C%A8-rails-%E5%B0%88%E6%A1%88%E5%A5%97%E7%94%A8-bootstrap-%E6%A8%A3%E5%BC%8F-e04f48bbecfb">如何在 Rails 專案套用 Bootstrap 樣式</a> was originally published in <a href="https://medium.com/its-boyu-s-note">It’s Boyu’s Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>