Rails 的超小型筆記 (2)


終於將昨晚的筆記整理完成了.但還是比較亂.
筆記也比較長.如果有錯.請指正

1
2
3
# Sqlite Console 的幫助
rails db
.help
1
2
3
4
# 切換到專案中
# 輸入 rails s 時提示需要安裝時
# 需確認 gem list 是否正確
rvm gemset use rails3.1.0
1
2
3
4
5
6
7
8
9
10
# 存取權限
讀寫: attr_accessor
可讀: attr_reader
可寫: attr_writer

白名單: attr_accessible
例子如: 可將存入的欄位 :username

黑名单: attr_protected
例子如: 不可以非法提交欄位 :role
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Devise 的簡單使用記錄
1. 在 Gemfile 中加入 gem 'devise'
2. bundle install
3. rails g devise:install
4. config/environments/development.rb 和 production.rb 加入

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

5. app/views/layouts/application.html.erb 加入

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

6. config/routes.rb 加開與設置首頁

root :to => "home#index"

7. rails g devise user
8. rails g devise:views
9. rake db:migrate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Devise 中加入 username 和 avatar 欄位
1. rails g migration add_username_into_users_table
2. 打開 db/migrate/[DATE]_add_username_into_users_table.rb
3. 修改 up 和 down 的內容為

def up
add_column :users, :username, :string, :limit => 30
end

def down
remove_column :users, :username
end

4. 執行 rake db:migrate 改變資料庫.使到 f.label 等函數中可使用這 :username 物件

----

1. rails g migration add_avatar_into_users_table
2. 打開 db/migrate/[DATE]_add_avatar_into_users_table.rb
3. 修改 up 和 down 的內容為

def up
add_column :users, :avatar, :integer, :default => 1
end

def down
remove_column :users, :avatar
end

4. 執行 rake db:migrate 改變資料庫.使到 f.label 等函數中可使用這 :avatar 物件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 添加驗證規則於 :username 的欄位中
1. 打開 user.rb
2. 驗證順序會由驗證語句的先後決定
3. 於 class User < ActiveRecord::Base 下面加入

# :username 不能為空,並且在 4 至 20 個字元以內,且是唯一的
validates_presence_of :username
validates_length_of :username, :in => 4..20
validates_uniqueness_of :username

# 另外也可以一行了結 rails 3.1 ?
validates :username,
:presence => true,
:length => { :minimum => 4, :maximum => 20 }
:uniqueness => true,

# 並於 attr_accessible 加入 :username
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Devise 中的註冊錯誤輸出覆寫
# Devise 中的註冊錯誤文字是以 devise_error_messages! 函數保存並不是 flash 變量
# 所以如果需要改寫樣式可以自寫模組覆蓋
1. 於 app/helpers 下編寫 devise_helper.rb
2. 內容如下:

module DeviseHelper
=begin
# 這是自行編寫的方法
def devise_error_messages!
return "" if resource.errors.empty?

resource.errors.full_messages.map do |msg|
return msg.html_safe
end
end
=end

# 這是原來的方法
def devise_error_messages!
return "" if resource.errors.empty?

messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t(
"errors.messages.not_saved",
:count => resource.errors.count,
:resource => resource.class.model_name.human.downcase
)

html = < <-HTML
<div id="error_explanation">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>

HTML

html.html_safe
end
end
1
2
3
4
5
6
7
8
# Rails 中輸出錯誤時 label 會以 div.field_with_errors 呈現
# 如想改為 span.field_with_errors 的可以通過
1. 打開 config/environment.rb
2. 添加

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
"<span class=\"field_with_errors\">#{html_tag}</span>".html_safe
end
1
2
3
# Devise 中的多國語系
cd config/locales
wget https://raw.github.com/gist/964066/342efba3dfd7ff7a346764b3136c4ecc029f6939/devise.zh-TW.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# SCSS 中的 mixin.css.scss 使用時出現 undefine 或 root document 的情況
# (可能自己的 scss 結構比較古怪.先寫 Dummy 和 SCSS 再整合到 rails 的古怪?)
1. 可以嘗試以下的結構方式解決

app/
assets/
scss/
sub_folder
_common.css.scss
_footer.css.scss
_header.css.scss
_main.css.scss
_mixin.css.scss
stylesheets
application.css
default.css.scss
reset.css

2. 而 application.css 中的 /= 則為,而個中的 require_directory 不是並要的

*= require_self
*= require reset
*= require_directory .
*= require_tree .

3. 而 default.css.scss 內容則為

@import "mixin";
@import "common";

body {
background: #FFF;
font-size: 12px;
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
color: #333;
line-height: 1.28;
text-align: left;
direction: ltr;
unicode-bidi: embed;
}

#wrapper {
@import "header";
@import "main";
@import "footer";
}
1
2
# 在 SCSS/CSS 中使用 assets/images 的圖片可以通過輔助函數
background: image_url("header-background.png");
1
2
# 在 erb 的模版中可以通過
<%= image_tag("external-link.gif") %>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 每個頁面中的多國語言化使用
1. erb 中可以通過 t(".caption") 的方式取得目前頁面該顯示的語言
2. 而其中的內容為字串的對應 path 如 t("home.title") 為

"zh_TW":
home:
title: "This is a test"

3. config/application.rb 中打開預設的 locale 和 timezone

config.time_zone = 'Hong Kong'
config.i18n.default_locale = "zh-TW"

4. 如果他是一個 view 頁面的內容如 views/layouts/application.html.erb
-- 裡面有 t(".login") 則 config/locales/zh_TW.yml 會存在

"zh_TW":
layouts:
application:
login: "登入"

5. 如果 config/locales/zh_TW.yml 裡面太過長.可以通過以下方法分拆 rails 和 custom 兩個語系
-- 打開 config/application.rb 加入

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', 'rails', '*.{rb,yml}').to_s]

-- 於 config/locales 下面加入 rails 目錄和 zh_TW.yml
-- 將原來的 zh_TW.yml 的 250 行左右往上的全部內容都掉到 locales/rails/zh_TW.yml 裡面
-- 註冊在原來的 locales/zh_TW.yml 加回 zh_TW: 這字眼
1
2
3
4
5
6
7
8
9
10
# Devise 中設定驗證失敗時跳到正確的語言介面
1. 打開 app/controllers/application_controller.rb 加入

def set_locale
I18n.locale = params[:locale]
end

def self.default_url_options(options={})
options.merge({ :locale => I18n.locale })
end
1
2
3
4
5
6
7
8
9
10
11
12
# 指定特別的 layout 頁面
1. 於 controller 裡面加入 layout "other"
2. 如果是指定的方法才使用此 layout 可以通過 layout "other", :only => :index
3. 或者可以直接在指定的方法中指定渲染 layout 可以 render :layout => "other"
4. 如果不需要 layout 的可以 render :layout => false
5. 如果要排除的可以於第 1 中加入 :except => ["logout", "login"] 指定排除

# 局部模版可以使用
# 其會以整倨 view 根目錄作為搜尋的開始
1. <%= render :partial => "shared/error_message" %>
2. <%= render :partial => "shared/error_message", :locals => { :a => 1, :b => 2 } %>
3. <%= render :partial => "shared/error_message", :collection => @list %>