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()

閱讀全文

Ruby 內建變數與內建常數

指令輸出 ``

可以直接使用 `` 符號,框住要行的指令,取得執行的結果,回傳形式為字串。範例為列出所有 .rb 結尾的檔案。

1
2
3
4
files = `ls`
files.split.each do |file|
print "#{file}\n" if file =~ /.rb$/i
end

閱讀全文

Ruby One-Liners

From Here

翻轉每一行

輸入資料:

1
2
3
4
$ cat foo
qwe
123
bar

翻轉每一行的字:

1
2
3
4
$ ruby -e 'File.open("foo").each_line { |l| puts l.chop.reverse }'
ewq
321
rab

閱讀全文