Ruby on Rails

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

閱讀全文

Ruby on Rails Debug ByeBug 使用

使用

在程式碼中想要中斷的地方加入 byebug,程式執行到 byebug 這個位置則會停下來讓使用者 debug。而至於進入 Debug 頁面能幹麻,大致上跟 GDB Debug 差不多,一步一步的執行,然後檢查每一個參數的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 進入 byebug 頁面
[9, 18] in /Users/akiicat/someplace.rb
9: byebug
10:
11: @message.save!
12:
13:
=> 14: @path = conversation_path(@conversation)
15: end
16:
17: private
18:
(byebug) d @path

閱讀全文