Ruby on Rails Assets Pipeline 的使用方式

Assets Pipeline

config/initializers/assets.rb 中把 assets 加入 precompile 的路徑,不建議使用 *.css *.js 加入所有的東西,因為會將一些有的沒的或是不需要的東西一起編譯進去。

assets pipeline 的主要好處就是把所有的 css 包成一個檔案,漸少 request 的數量,像是 application.css 會載入所有被 require 的檔案,最後只需要傳送一個 css 就行了,同理 javascript 也是如此。

Layout

看不懂在說什麼,直接看例子比較快。

假設現在我們的 layout 裡面有兩個不同的頁面 admin.html.erb 跟 user.html.erb,要分別讓他們使用不同的 css js。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
app
├--assets
│ ├--javascripts
│ │ ├--admin.js
│ │ ├--user.js
│ │ └--...
│ └--stylesheets
│ ├--admin.scss
│ ├--user.scss
│ └--...
└--view
└--layout
├--admin.html.erb
└--user.html.erb

但為了減少請求的數量,我們讓一個 layout 只需要一個 css 和一個 js,在 layout 中的話會像這樣:

1
2
3
4
5
6
7
# app/views/layouts/admin.html.erb
<%= stylesheet_link_tag 'admin', media: 'all' %>
<%= javascript_include_tag 'admin' %>

# app/views/layouts/user.html.erb
<%= stylesheet_link_tag 'user', media: 'all' %>
<%= javascript_include_tag 'user' %>

如果使用 admin 的 layout 就會載入 admin.css admin.js

如果使用 user 的 layout 就會載入 user.css user.js

閱讀全文

Ruby on Rails 在 production 環境下跑 rails 5 專案

Production Mode

資料庫 Migrate

migrate production 環境下的資料庫,至於設定檔就不在這邊多說了。

1
RAILS_ENV=production rake db:migrate

產生 Secret Key

config/secret.yml 這個檔案裡面需要 production 環境的 secret key。

1
2
3
# config/secret.yml
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

用終端機產生 secret key,然後放置環境變數中。

1
2
3
4
5
# 產生 secret key
rake secret

# 將剛剛產生出來的 secret key 放入環境變數中
export SECRET_KEY_BASE=$(rake secret)

閱讀全文

Ruby on Rails Devise 與 註冊時輸入使用者名稱

註冊時輸入使用者名稱

在產生 rails generate devise:views users 後,然後在註冊的地方增加 username 的欄位。

1
2
3
4
5
# app/views/users/registrations/new.html.erb
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, autofocus: true %>
</div>

在 controller 增加允許使用者欄位的驗證。

1
2
3
4
5
6
7
8
9
10
11
12
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
end

閱讀全文

Ruby on Rails 設定 Devise 與 註冊 登入 登出

Gemfile

測試環境為 rails 5

1
gem 'devise'

然後執行 bundle

Configuration

安裝 devise 的設定檔

1
rails generate devise:install

然後在 config/environments/development.rb 加上寄信認證的設定

1
2
3
# config/environments/development.rb
# config/environments/test.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

閱讀全文

Ruby on Rails 中使用 Rspec

Gemfile

1
2
3
group :development, :test do
gem 'rspec-rails'
end

然後執行 bundle

Initialize

建立新的 rspec 檔

1
rails generate rspec:install

閱讀全文

Ruby on Rails 用 Factory Girl 自動產生 rspec 測資

安裝

Gemfile 加上以下的部分,然後執行 bundle

1
2
3
4
# Gemfile
group :development, :test do
gem 'factory_girl_rails', '~> 4.0'
end

設定

spec/support/factory_girl.rb 新增一個檔案,且加入以下的東西。

1
2
3
4
5
6
7
# RSpec
## spec/support/factory_girl.rb
require 'factory_girl_rails'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end

然後在 spec/spec_helper.rb 的最前面加入以下行,使用 factory_girl 的設定檔。

1
2
## spec/spec_helper.rb
require 'support/factory_girl'

閱讀全文

Ruby on Rails 中使用 UUID primary key

PostgreSQL

在 PostgreSQL 中有支援 UUID 為唯一 ID,所以在 PostgreSQL 使用 UUID 是相對簡單的。在
migration 裡面,我們要告訴 PostgreSQL 使用 UUID extension,這樣能夠讓 PostgreSQL
自動對每一個物件建立唯一的 UUID,而不是讓 Ruby On Rails 花費額外的時間來處理。

使用 PostgreSQL 前在 Gemfile 中加上以下這行。

1
2
# Gemfile
gem 'pg'

設定 adapter 為 postgresql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
default: &default
adapter: postgresql
encoding: unicode
host: localhost
port: 5432
pool: 5
username: akii
password: <%= ENV['PG_PASSWORD'] %>

development:
<<: *default
database: development

test:
<<: *default
database: test

production:
<<: *default
database: production
# pool: 5
# url: <%= ENV['DATABASE_URL'] %>

設定 postgresql 的密碼

1
export PG_PASSWORD=xxxxxx

閱讀全文

PostgreSQL 安裝

更新 homebrew

1
brew update & brew upgrade

安裝 PostgreSQL

1
brew install postgresql

建立 PostgreSQL

1
initdb /usr/local/var/postgres

閱讀全文

Ruby Mix-in Module include 的規則

繼承關係

Ruby 只能繼承唯一一個 parent 的單純繼承,但藉由 Mix-in 機制,可以在單純繼承的架構下,在多個類別之間共享一些工能。

1
2
3
4
5
class Book
include Comparable
end

Book.ancestors # => [Book, Comparable, Object, Kernel]

Comparable 雖然不是 parent,但是運作情形差不多

1
2
3
4
5
6
7
8
9
10
11
┌────────────┐                                    ┌────────────┐
│ Object │ │ Object │
└────────────┘ └────────────┘
↑ ↑
│ ┌────────────┐ ┌────────────┐
│←──────────│ Comparable │ │ Comparable │
│ └────────────┘ └────────────┘
│ ↑
┌────────────┐ ┌────────────┐
│ Book │ │ Book │
└────────────┘ └────────────┘

閱讀全文

Ruby 多重指派 Multiple assignment

多重指派 Multiple assignment

一口氣做不只一個指派的動作時

盡量用在有關係的變數上,互相沒有關係的多重指派,只會讓程式很難懂。

1
a, b, c = 1, 2, 3

回傳方法不只一個

定義 Ruby 的方法時,有時候會想要傳回不只一個物件。這時傳回值也可以使用多重指派的方式取得回傳值。

1
2
3
4
5
6
def location()
...
return x, y, z
end

a, b, c = location()

閱讀全文