Rails Helpers

download Rails Helpers

of 25

  • date post

    05-Apr-2018
  • Category

    Documents

  • view

    217
  • download

    0

Embed Size (px)

Transcript of Rails Helpers

  • 7/31/2019 Rails Helpers

    1/25

    Rails HelpersJohnson Liang / MrOrz

  • 7/31/2019 Rails Helpers

    2/25

  • 7/31/2019 Rails Helpers

    3/25

  • 7/31/2019 Rails Helpers

    4/25

    Helpers

  • 7/31/2019 Rails Helpers

    5/25

    Helper

    link_to, stylesheet_link_tag,auto_link,

    Named Helper posts_path, edit_post_url(:id)

    Form Helper

    form_for, text_field,

  • 7/31/2019 Rails Helpers

    6/25

    http: / / ihower.t w/ rails3/ actionview-helpers.html

    htt p:/ / blog.xdit e.net / posts/ 2011/ 12/ 08/ how-to-design-helpers/

    htt p:/ / blog.xdit e.net / posts/ 2011/ 12/ 09/ how-to-design-helpers-2/

    htt p:/ / blog.xdit e.net / posts/ 2012/ 01/ 15/ how-to-design-helper-3/

    http://ihower.tw/rails3/actionview-helpers.htmlhttp://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/http://blog.xdite.net/posts/2012/01/15/how-to-design-helper-3/http://blog.xdite.net/posts/2011/12/09/how-to-design-helpers-2/http://blog.xdite.net/posts/2011/12/08/how-to-design-helpers/http://ihower.tw/rails3/actionview-helpers.html
  • 7/31/2019 Rails Helpers

    7/25

    helper

    view

    rails consolehelper.

    1.9.2-p290 :005 > helper.link_to 'Google', 'http://google.com', :target=>_blank

    => "Google"

  • 7/31/2019 Rails Helpers

    8/25

    link_to(*args, &block)

    htt p:/ / api. rubyonrails.org/ classes/ ActionView/ Helpers/ UrlHelper.html#method-i-l ink_to

    Named helper, covered later

    http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
  • 7/31/2019 Rails Helpers

    9/25

    link_to(*args, &block)

    htt p:/ / api. rubyonrails.org/ classes/ ActionView/ Helpers/ UrlHelper.html#method-i-l ink_to

    Handled by jquery.ujs.js (the jquery-rails gem)

    http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
  • 7/31/2019 Rails Helpers

    10/25

    javascript_include_tag(*sources)

    stylesheet_link_tag(*sources)

    j s app/ assets/ j avascripts

    css app/ assets/ stylesheets

    http:/ / goo.gl/ J30Kg htt p:/ / goo.gl/ OoluN

    http://goo.gl/J30Kghttp://goo.gl/OoluNhttp://goo.gl/OoluNhttp://goo.gl/J30Kg
  • 7/31/2019 Rails Helpers

    11/25

    image_tag(source, opt ions = {})

    app/ assets/ images

    htt p:/ / api.rubyonrails.org/ classes/ Act ionView/ Helpers/ AssetTagHelper.html

    http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
  • 7/31/2019 Rails Helpers

    12/25

    javascript_include_tagstylesheet_link_tag

    image_tag

    WHY?

  • 7/31/2019 Rails Helpers

    13/25

    javascript_include_tagstylesheet_link_tag

    image_tag

    asset server

    Product ion mode

  • 7/31/2019 Rails Helpers

    14/25

    truncate(text, options = {})

    simple_format (text, html_options={}, options={})

    html

    htt p:/ / api. rubyonrails.org/ classes/ ActionView/ Helpers/ TextHelper.html

    http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.htmlhttp://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html
  • 7/31/2019 Rails Helpers

    15/25

    ht tp:/ / ihower.t w/ rails3/ rout ing.html

    http://ihower.tw/rails3/routing.htmlhttp://ihower.tw/rails3/routing.html
  • 7/31/2019 Rails Helpers

    16/25

    *_url, *_path

    controller view

    rails consoleapp.

    1.9.2-p290 :019 > app.posts_path

    => "/posts"

    1.9.2-p290 :020 > app.posts_url

    => "http://www.example.com/posts"

  • 7/31/2019 Rails Helpers

    17/25

    Named Routesrake routes

    RouteName HTTP verbMatches all if empty URL patternto matchRoutingparameters

  • 7/31/2019 Rails Helpers

    18/25

    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"

  • 7/31/2019 Rails Helpers

    19/25

    htt p:/ / ihower.t w/ rails3/ actionview-helpers.html

    htt p:/ / guides.rubyonrails.org/ form_helpers.html

    http://ihower.tw/rails3/actionview-helpers.htmlhttp://guides.rubyonrails.org/form_helpers.htmlhttp://guides.rubyonrails.org/form_helpers.htmlhttp://ihower.tw/rails3/actionview-helpers.html
  • 7/31/2019 Rails Helpers

    20/25

    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_tagsubmit_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

  • 7/31/2019 Rails Helpers

    21/25

    FormHelper FormTagHelper

    form_for CSRF name

    form_tag CSRF tag form_forFormHelper FormHelper

    FormTagHelper name

  • 7/31/2019 Rails Helpers

    22/25

    .html.erb .html

    IE UTF-8 bug

    CSRF

    style

    hash

    /people"

  • 7/31/2019 Rails Helpers

    23/25

    .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

  • 7/31/2019 Rails Helpers

    24/25

    FormHelper

    @post newid

    @post

    @post

    FormHelpername post[xxx]

    Named Helper html

  • 7/31/2019 Rails Helpers

    25/25

    References Helper

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

    Form Helpers htt p:/ / ihower. tw/ rails3/ actionview-helpers.html http:/ / guides.rubyonrails.org/ form_helpers.html

    Named Route Helpers ht tp:/ / ihower.t w/ rails3/ rout ing.html

    Test ing Helpers Under Rails Console http:/ / stackoverflow.com/ quest ions/ 151030/ how-do-i-call -controller-view-

    methods-from-the-console-in-rails http:/ / stackoverflow.com/ quest ions/ 4400888/ rails-call ing-a-controller-act ion-

    from-the-consolehttp:/ / stackoverf low.com/ quest ions/ 2846247/ rails-check-output-of-path-helper-from-console

    http://ihower.tw/rails3/actionview-helpers.htmlhttp://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/http://ihower.tw/rails3/actionview-helpers.htmlhttp://guides.rubyonrails.org/form_helpers.htmlhttp://ihower.tw/rails3/routing.htmlhttp://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-railshttp://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-railshttp://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-railshttp://stackoverflow.com/questions/4400888/rails-calling-a-controller-action-from-the-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-consolehttp://stackoverflow.com/questions/4400888/rails-calling-a-controller-action-from-the-consolehttp://stackoverflow.com/questions/4400888/rails-calling-a-controller-action-from-the-consolehttp://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-railshttp://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-railshttp://ihower.tw/rails3/routing.htmlhttp://guides.rubyonrails.org/form_helpers.htmlhttp://ihower.tw/rails3/actionview-helpers.htmlhttp://blog.xdite.net/posts/2012/01/15/how-to-design-helper-3/http://blog.xdite.net/posts/2011/12/09/how-to-design-helpers-2/http://blog.xdite.net/posts/2011/12/08/how-to-design-helpers/http://ihower.tw/rails3/actionview-helpers.html