Tuesday 25 December 2012

Rails helpers you should be familiar with

AssetTagHelper

- helps you link html page with various assets as javascripts, stylsheets, images...
- useful if you have assets saved on different servers

javascript_include_tag(*sources)

- creates a script tag for each of the provided sources
- tarts from public/javascripts
- uses javascript_path to calculate the path to the javascript file

stylesheet_link_tag(*sources)

- creates a stylesheet link tag for each of the provided sources
- starts from public/stylesheets
- stylesheet_path caluculates locat

image_tag(source, options = {})

- creates a image tag for the provided image location
- image_path caluculates location
- aditional HTML params(align...) can be passed by using options hash

CaptureHelper

- very useful for reusing page parts at different locations by using yield

content_for(name, &block)
- saves the content of the block and provides it for later usage
- useful for action menus or similar components used at different
places but with similar functions

content_for?(name)

- checks weather we'll get any context by using content_for 

DateHelper

- useful for creating select tags for different date combos(full date, only year, time ...)
by using methods like select_date, select_time ...
- includes distance calculation methods like distance_of_time_in_words:
  distance_of_time_in_words(Time.now, Time.now + 5.minute) => "5 minutes" 

FormHelper

- helps with craeating forms closely related to Active Record models linked with a view

form_for(record, options = {}, &proc)

- creates a form and a scope around a specific model object that is used as a source for field data.
- accepts params :url (location where form is submitted to), :html (standard attributes) and :builder (custom form builder e.g. Formtastic)
- uses automated resource identification to configure submit location
- default protection from CSRF attacks
- support for multiple objects update by using square bracket notation => form_for "user[]" do ...

fields_for(record_name, record_object = nil, options = {}, &block)

- creates a scope arround a specific model object without creating tags
form_for @user do |f|
.....
fields_for @user.settings do |s| => params[:user][:settings]

field builders

check_box, email_field, file_field, hidden_field, label, number_field, password_field, radio_button, range_field, search_field, submit, telephone_field, text_area and text_field 

FormTagHelper

- same results as FormHelper but without a link to Active Model instance

NumberHelper

- helps with formatting numbers understandable to user

number_to_currency(number, options = {})

- number_to_currency(12345.60) => $ 12,345.60

number_to_human_size(number, options = {})

- number_to_human_size(1234) => 1.2 KB

 

TagHelper

content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) 

- programmatically creates a HTML block by using provided tag name and content
- you can also pass a block to create more complex content

tag(name, options = nil, open = false, escape = true)  

-creates tag programmatically

 

TextHelper


pluralize - attempts to pluralize singular world

 

UrlHelper

- methods for generating links

button_to(name, options = {}, html_options = {})

- creates a form submit button directed at the URL from options parameter
- used for all resource modifier actions to prevent unwanted changes

link_to(*args, &block)

- creates a link for the given name and options
- options are processed by url_for method which returns URL for the provided options

No comments:

Post a Comment