Skip to content

Commit

Permalink
add 6-2
Browse files Browse the repository at this point in the history
  • Loading branch information
xdite committed Nov 10, 2013
1 parent 5a18da2 commit c03a6d9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
2 changes: 1 addition & 1 deletion manuscript/chapter-01-00.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* 學會設定 Route
* 學會 resources 的設定 (單層 resources)
* 對 Rails RESTful 有初步的理解
* 知道 before_filter 使用的場景,並如何應用
* 知道 before_action 使用的場景,並如何應用



Expand Down
38 changes: 19 additions & 19 deletions manuscript/chapter-03-0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,33 @@ devise 的 view 預設是隱藏起來,直接使用 gem 內的 view。要客製

~~~~~~~~~
invoke Devise::Generators::SharedViewsGenerator
create app/views/users/shared
create app/views/users/shared/_links.erb
create app/views/devise/shared
create app/views/devise/shared/_links.erb
invoke simple_form_for
create app/views/users/confirmations
create app/views/users/confirmations/new.html.erb
create app/views/users/passwords
create app/views/users/passwords/edit.html.erb
create app/views/users/passwords/new.html.erb
create app/views/users/registrations
create app/views/users/registrations/edit.html.erb
create app/views/users/registrations/new.html.erb
create app/views/users/sessions
create app/views/users/sessions/new.html.erb
create app/views/users/unlocks
create app/views/users/unlocks/new.html.erb
create app/views/devise/confirmations
create app/views/devise/confirmations/new.html.erb
create app/views/devise/passwords
create app/views/devise/passwords/edit.html.erb
create app/views/devise/passwords/new.html.erb
create app/views/devise/registrations
create app/views/devise/registrations/edit.html.erb
create app/views/devise/registrations/new.html.erb
create app/views/devise/sessions
create app/views/devise/sessions/new.html.erb
create app/views/devise/unlocks
create app/views/devise/unlocks/new.html.erb
invoke erb
create app/views/users/mailer
create app/views/users/mailer/confirmation_instructions.html.erb
create app/views/users/mailer/reset_password_instructions.html.erb
create app/views/users/mailer/unlock_instructions.html.erb
create app/views/devise/mailer
create app/views/devise/mailer/confirmation_instructions.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
~~~~~~~~~

{::pagebreak :/}

### 修改註冊表單

註冊表單的檔案是 `app/views/users/registrations/new.html.erb`,加入一行 name 使之成為
註冊表單的檔案是 `app/views/devise/registrations/new.html.erb`,加入一行 name 使之成為

~~~~~~~~~
<h2>Sign up</h2>
Expand Down
1 change: 1 addition & 0 deletions manuscript/chapter-04-1-2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
在一般的認知中,Group 的開創者應該就要是 group 的一員。這有兩種作法,一種是到 `app/controller/groups_controller.rb` 裡的 `create` action 裡面加入 `current_user.join!(group)`

~~~~~~~~~~
def create
@group = current_user.groups.build(group_params)
if @group.save
current_user.join!(group)
Expand Down
2 changes: 1 addition & 1 deletion manuscript/chapter-05-1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

~~~~~~~~
class Account::GroupsController < ApplicationController
before_filter :login_required
before_action :login_required

def index
@groups = current_user.participated_groups
Expand Down
2 changes: 1 addition & 1 deletion manuscript/chapter-05-2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
~~~~~~~~
class Account::PostsController < ApplicationController

before_filter :login_required
before_action :login_required

def index
@posts = current_user.posts
Expand Down
2 changes: 1 addition & 1 deletion manuscript/chapter-05-3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~~~~~~~~
class Account::PostsController < ApplicationController

before_filter :login_required
before_action :login_required

def index
@posts = current_user.posts.order("updated_at DESC")
Expand Down
63 changes: 63 additions & 0 deletions manuscript/chapter-06-2.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
## Ch 6.2 自己撰寫的 helper 包裝 html

Helper 是一些使用在 Rails 的 View 當中,用 Ruby 產生/整理 HTML code 的一些小方法。通常被放在 `app/helpers` 下。預設的 Helper 名字是對應 Controller 的,產生一個 Controller 時,通常會產生一個同名的 Helper。如 `PostsController` 與 `PostsHelper`。

### 使用情境

使用 Helper 的情境多半是:

* 產生的 HTML code 需要與原始程式碼進行一些邏輯混合,但不希望 View 裡面搞得太髒。
* 需要與預設的 Rails 內建的一些方便 Helper 交叉使用。

使用 Helper 封裝程式碼可以帶給專案以下一些優點:

* Don't repeat yourself(DRY)程式碼不重複
* Good Encapsulation好的封裝性
* 提供 view 模板良好的組織
* 易於修改程式碼

### 範例

在剛剛的專案當中,顯示 Post 的程式碼如下:

~~~~~~~~~
<%= @post.content %> %>
~~~~~~~

隨著專案變遷,這樣的程式碼,可能會依需求改成:

(需要內容斷行)

~~~~~~~~
<%= simple_format(@post.content) %> %>
~~~~~~~~

之後又改成 (只顯示頭一百字)

~~~~~~~~
<%= truncate(simple_format(@post.content), :lenth => 100) %> %>
~~~~~~~~

最後又改成 (內容若有網址需要自動超連結)

~~~~~~~~
<%= auto_link(truncate(simple_format(@post.content), :lenth => 100)) %>
~~~~~~~~

而麻煩的是,這樣類似的內容,常常在專案出現。每當需求變更,開發者就需要去找出來,有十個地方,就需要改十遍,很是麻煩。

Helper 就是用在這樣的地方。與其一開始寫下

~~~~~~~~~
<%= @post.content %> %>
~~~~~~~

不如,一開始就設計一個 Helper ` <%= render_post_content(@post) %>`

~~~~~~~~~
def render_post_content(post)
auto_link(truncate(simple_format(@post.content), :lenth => 100))
end
~~~~~~~

以後變更需求就只要修改一個地方即可。


## Ch 6.3 使用 partial 整理 html

## Ch 6.4 使用 scope 整理 query

0 comments on commit c03a6d9

Please sign in to comment.