Slide 1

Slide 1 text

Rails Helpers Johnson Liang / MrOrz

Slide 2

Slide 2 text

咦?

Slide 3

Slide 3 text

咦?

Slide 4

Slide 4 text

咦? Helpers

Slide 5

Slide 5 text

Helper • 輔助方法(函式) • 常用的 ▫ link_to, stylesheet_link_tag, auto_link, …… • Named Helper ▫ posts_path, edit_post_url(:id) • Form Helper ▫ form_for, text_field, ……

Slide 6

Slide 6 text

http://ihower.tw/rails3/actionview-helpers.html http://blog.xdite.net/posts/2011/12/08/how-to-design-helpers/ http://blog.xdite.net/posts/2011/12/09/how-to-design-helpers-2/ http://blog.xdite.net/posts/2012/01/15/how-to-design-helper-3/

Slide 7

Slide 7 text

以下的 helper…… • 只能在 view 裡頭呼叫 • 在 rails console 裡使用,前面需加上 helper. 1.9.2-p290 :005 > helper.link_to 'Google', 'http://google.com', :target=>’_blank’ => "Google"

Slide 8

Slide 8 text

link_to(*args, &block) • 語法 • 範例 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to Named helper, covered later

Slide 9

Slide 9 text

link_to(*args, &block) • 語法 • 範例 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to Handled by jquery.ujs.js (the jquery-rails gem)

Slide 10

Slide 10 text

javascript_include_tag(*sources) stylesheet_link_tag(*sources) • js 檔放 app/assets/javascripts • css 檔放 app/assets/stylesheets http://goo.gl/J30Kg http://goo.gl/OoluN

Slide 11

Slide 11 text

image_tag(source, options = {}) • 影像檔放 app/assets/images http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

Slide 12

Slide 12 text

javascript_include_tag stylesheet_link_tag image_tag WHY?

Slide 13

Slide 13 text

javascript_include_tag stylesheet_link_tag image_tag 以後把 asset 傳到不同 server,只要改一行: Production mode 時自動壓縮:

Slide 14

Slide 14 text

truncate(text, options = {}) simple_format(text, html_options={}, options={}) • 自動加上…… • 清除有害 html 標籤 http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html

Slide 15

Slide 15 text

http://ihower.tw/rails3/routing.html

Slide 16

Slide 16 text

*_url, *_path • 可以在 controller 和 view 裡頭呼叫 • 在 rails console 裡使用,前面需加上 app. 1.9.2-p290 :019 > app.posts_path => "/posts" 1.9.2-p290 :020 > app.posts_url => "http://www.example.com/posts"

Slide 17

Slide 17 text

Named Routes rake routes Route Name HTTP verb Matches all if empty URL pattern to match Routing parameters

Slide 18

Slide 18 text

Named Helper • _path 與 _url • 傳參數 posts_path => "/posts" posts_url => "http://www.example.com/posts" edit_post_comment_path :id=>4, :post_id=>2 => "/posts/2/comments/4/edit"

Slide 19

Slide 19 text

http://ihower.tw/rails3/actionview-helpers.html http://guides.rubyonrails.org/form_helpers.html

Slide 20

Slide 20 text

有兩種 form_tag button_tag check_box_tag field_set_tag file_field_tag hidden_field_tag label_tag password_field_tag radio_button_tag search_field_tag submit_tag text_area_tag text_field_tag FormHelper FormTagHelper form_for check_box file_field hidden_field label password_field radio_button search_field text_area text_field

Slide 21

Slide 21 text

有兩種 FormHelper FormTagHelper • 包在 form_for 裡 • CSRF 防禦 • 綁資料庫物件 • 自動處理 name 屬性 • 包在 form_tag 裡 • CSRF 防禦 • 單純產生 tag • 也能用在 form_for 裡 FormHelper FormHelper 另一種寫法 FormTagHelper 得自己指定name 屬性

Slide 22

Slide 22 text

.html.erb  .html 舊 IE UTF-8 bug CSRF 防禦 讓你 style 它 自動決定網址 物件相關的放進 hash /people"

Slide 23

Slide 23 text

.html 送出  params[] params # => { :utf8 => “✔ ”, :authenticity_token => …, :person => { :first_name => …, :last_name => …, :biography => …, (:admin => “true”) }, :commit => “Create Post”, :controller => “person” } /people" 所以我們可以 @person = Person.new(params[:person]) 來自動填入欄位! (前提是 attr_accessible 要設好)

Slide 24

Slide 24 text

FormHelper 的神奇引數 若 @post 剛被 new 出來 (沒有 id 屬性) 若 @post 是從資料庫撈出來的 依 @post 不同,會擇一展開為下列 指定FormHelper 的 name 為 post[xxx] Named Helper 幫你加的 html 屬性

Slide 25

Slide 25 text

References • 常用的 Helper ▫ http://ihower.tw/rails3/actionview-helpers.html ▫ http://blog.xdite.net/posts/2011/12/08/how-to-design-helpers/ ▫ http://blog.xdite.net/posts/2011/12/09/how-to-design-helpers-2/ ▫ http://blog.xdite.net/posts/2012/01/15/how-to-design-helper-3/ • Form Helpers ▫ http://ihower.tw/rails3/actionview-helpers.html ▫ http://guides.rubyonrails.org/form_helpers.html • Named Route Helpers ▫ http://ihower.tw/rails3/routing.html • Testing Helpers Under Rails Console ▫ http://stackoverflow.com/questions/151030/how-do-i-call-controller-view- methods-from-the-console-in-rails ▫ http://stackoverflow.com/questions/4400888/rails-calling-a-controller-action- from-the-consolehttp://stackoverflow.com/questions/2846247/rails-check- output-of-path-helper-from-console