Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rails 3 Helpers
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Johnson Liang
May 10, 2012
Programming
200
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails 3 Helpers
CCSP 2012 Spring slides.
Johnson Liang
May 10, 2012
More Decks by Johnson Liang
See All by Johnson Liang
Political Promise Tracker 政治承諾追蹤網 g0v-hackath14n 提案
mrorz
0
260
群眾協作的一些事—— 「自經區正反意見比較表」到 「臺北市長選舉承諾一覽表」到 「PPT 政治承諾追蹤網」
mrorz
0
580
Political Promise Tracker 政治承諾追蹤網 g0v-hackath12n 提案
mrorz
0
960
[Thesis Defense] SeeSS: Instant Change Impact Visualization for CSS developers
mrorz
0
300
g0v-hackath10n 提案 -- 2014 台北市長政見比較表
mrorz
0
310
網路及平台服務程式設計及零時政府 @ 創新網路技術推廣說明會
mrorz
0
250
g0v-hackath9n 提案 -- 自經區正反意見比較表
mrorz
0
180
CCSP 2014 期末成果展行前須知
mrorz
0
490
Google Analytics @ 102-2 CCSP
mrorz
0
390
Other Decks in Programming
See All in Programming
[2026-10-09]一枚の布で作らなくてもいい。 信頼性は「パッチワーク」で紡がれる~既存の運用と新しい可能性をつなぎ合わせてきたCREの話~
tosite
0
100
AIに “答え“を書かせるのを やめてみた
___akari11
0
120
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
510
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
110
ActionController meets Protocol Buffers
sorah
0
560
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
190
最近”作らせてる” ローカルAIアプリの話
akit37
0
480
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.6k
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
3
780
JIS keyboard support in Omarchy
lef237
0
140
Jetpack Compose Mechanisms
skydoves
3
830
Featured
See All Featured
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
310
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1k
Ethics towards AI in product and experience design
skipperchong
2
440
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Are puppies a ranking factor?
jonoalderson
2
4.1k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
320
GraphQLとの向き合い方2022年版
quramy
50
15k
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
740
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
Navigating Team Friction
lara
192
16k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Transcript
Rails Helpers Johnson Liang / MrOrz
咦?
咦?
咦? Helpers
Helper • 輔助方法(函式) • 常用的 ▫ link_to, stylesheet_link_tag, auto_link, ……
• Named Helper ▫ posts_path, edit_post_url(:id) • Form Helper ▫ form_for, text_field, ……
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/
以下的 helper…… • 只能在 view 裡頭呼叫 • 在 rails console
裡使用,前面需加上 helper. 1.9.2-p290 :005 > helper.link_to 'Google', 'http://google.com', :target=>’_blank’ => "<a href=\"http://google.com\" target=\"_blank\">Google</a>"
link_to(*args, &block) • 語法 • 範例 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to Named helper, covered
later
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)
javascript_include_tag(*sources) stylesheet_link_tag(*sources) • js 檔放 app/assets/javascripts • css 檔放 app/assets/stylesheets
http://goo.gl/J30Kg http://goo.gl/OoluN
image_tag(source, options = {}) • 影像檔放 app/assets/images http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
javascript_include_tag stylesheet_link_tag image_tag WHY?
javascript_include_tag stylesheet_link_tag image_tag 以後把 asset 傳到不同 server,只要改一行: Production mode 時自動壓縮:
truncate(text, options = {}) simple_format(text, html_options={}, options={}) • 自動加上…… •
清除有害 html 標籤 http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html
http://ihower.tw/rails3/routing.html
*_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"
Named Routes rake routes Route Name HTTP verb Matches all
if empty URL pattern to match Routing parameters
Named Helper • <ROUTE NAME>_path 與 <ROUTE NAME>_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"
http://ihower.tw/rails3/actionview-helpers.html http://guides.rubyonrails.org/form_helpers.html
有兩種 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
有兩種 FormHelper FormTagHelper • 包在 form_for 裡 • CSRF 防禦
• 綁資料庫物件 • 自動處理 name 屬性 • 包在 form_tag 裡 • CSRF 防禦 • 單純產生 tag • 也能用在 form_for 裡 FormHelper FormHelper 另一種寫法 FormTagHelper 得自己指定name 屬性
.html.erb .html 舊 IE UTF-8 bug CSRF 防禦 讓你
style 它 自動決定網址 物件相關的放進 hash /people"
.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 要設好)
FormHelper 的神奇引數 若 @post 剛被 new 出來 (沒有 id 屬性)
若 @post 是從資料庫撈出來的 依 @post 不同,會擇一展開為下列 指定FormHelper 的 name 為 post[xxx] Named Helper 幫你加的 html 屬性
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