Chester Tang's Blog 
  • Home
  • Archives
  • Categories
  •   
  •   

冒險村21 - draper

21 - Draper上篇 Design Pattern(1) - Decorator 簡單的介紹了自己生一個 decorator,不過可以發現東西會經過 helper,相對於來說個人不太喜歡,又或者是原本沒有使用 decorator 將邏輯直接放在 helper 內,且 full_name 方法是沒有任何 namespace 在 controller 與 view 之中。 1234# app/h

2021-10-06
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村20 - Design Pattern(1) - Decorator

20 - Design Pattern(1) - DecoratorDecorator pattern 的原理是,增加一個修飾類包裹原來的 class,包裹的方式是在修飾類的構造函數中將原來的類以參數的形式傳入。裝飾類實現新的功能,但是,在不需要用到新功能的地方,它可以直接調用原來的類中的方法。修飾類必須和原來的類有相同的接口。 直接來看如何使用: Create app/decorators fo

2021-10-05
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村19 - Activerecord-import

19 - Activerecord-import在寫入資料的時候,時常會有大量資料一次 import 的情況,可能是匯入整個 csv 檔案到資料表、或者是接收 api 將資料匯入。可能會使用 transaction 的方式來寫入確保資料都有正常匯入。 舉例來說,一次想入一筆資料: 1234567891011121314151617182.times.each do |time| Post.cre

2021-10-04
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村18 - Config

18 - Config這篇將介紹與上篇 Configatron 相同功能類似的 Gem - Config。 gem install12# Gemfilegem "config" Installing1234567891011bundle exec rails rails g config:install# auto create- config/initializers/con

2021-10-03
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村17 - Configatron

17 - ConfigatronRails 內時常會用到共用的連結、字串、數字等 config,除了可以寫在 yml 來處理外,也可以安裝現成的 Gem 來使用,這篇將介紹 Configatron,下一篇則是 Config。 gem install12# Gemfilegem "configatron" Installing12345678bundle exec rails g

2021-10-02
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村16 - customize breadcurmb

16 - customize breadcurmb延續上篇 customize tooltips with data attribute 後,另外也來自己手刻一個麵包屑吧~ 可能想說套個 bootstrap 像是這樣子: 1234567891011121314151617181920<nav aria-label="breadcrumb"> <ol clas

2021-10-01
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村15 - customize tooltips with data attribute

15 - customize tooltips with data attribute雖然這次想介紹的東西偏向後端為主,不過畢竟寫 ROR 除非前後端完全分離,不然多少還是會碰到前端一些,想到 tooltips 基本上是每個網站多多少少會有的功能,但如果是用 bootstrap 或者其他的套件可能就會踩到 Turbolinks 的雷,又或者是還要載入 Enable tooltips ever

2021-09-30
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村14 - counter cache

14 - counter cache在許多情況下,會需要統計一對多關聯的資料數量。舉例來說像是 User has_many Post。這時如果要統計該 user 擁有的 post 數量時,若直接使用 User.posts.size 則每次讀取頁面都會重新再統計一次,影響網站效能,而 ActiveRecord’s counter_cache 可以來幫助解決這個問題。 以 User has_man

2021-09-29
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村13 - migration notes

13 - migration notesDatabase GUI上個專案跟這個專案剛好 db 都是用 mysql,分享一下匯入資料的方法,還有一些 migration 問題的一些筆記,另外個人滿推以下兩種 database 的 GUI: TablePlus Sequal Pro 兩者都滿好用的也其實都差不多,因為同事都用 Sequal Pro 還有個人剛好也偏好,所以後來主要都是用 Sequ

2021-09-28
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails

冒險村12 - rescue exception

12 - rescue exception異常處理在開發過程中時常可見,舉例來說 Rails 中找不到對應的 routing 的時候則會丟出 Routing Error,或者是在特定的程式碼中手動加入 raise 來丟出 Exception 123456# 驗證 params[:user]def validate_user if user.blank? raise "User i

2021-09-27
13th鐵人賽
13th鐵人賽 後端冒險村 ruby on rails
1234…13

Search

Copyright © 2021 - Chester Tang.