cheatsheets/rails-helpers.md

78 lines
2.1 KiB
Markdown

title: Rails helpers
---
### Date helpers
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
### Date
distance_of_time_in_words(Time.now, project.end_date) #=> 3 hours
distance_of_time_in_words_to_now project.end_date #=> 3 hours
### Time select
# Creates a time select tag that, when POSTed, will be stored in the article
# variable in the sunrise attribute.
time_select "article", "start_time"
# All options are optional
time_select "article", "start_time", \
:include_seconds => true,
:minute_step => 15,
:prompt => true,
:prompt => { :hour => "Choose hr", :minute => "Choose min", :second => "Choose sec" },
:ampm => true
# For dates (all options are optional)
date_select "article", "written_on", \
:start_year => 1995,
:use_month_numbers => true,
:discard_day => true,
:include_blank => true,
:order => [:day, :month, :year],
:default => 3.days.from_now,
:default => { :day => 20 },
:prompt => { :day => 'Select day', :month => 'Select month', :year => 'Select year' }
### Time tag
time_tag Date.today #=> <time datetime="2010-11-04">November 04, 2010<%rtime>
time_tag Time.now #=> <time datetime="2010-11-04T17:55:45+01:00">November 04, 2010 17:55</time>
time_tag Date.yesterday, 'Yesterday' #=> <time datetime="2010-11-03">Yesterday<%rtime>
time_tag Date.today, :pubdate => true #=> <time datetime="2010-11-04" pubdate="pubdate">November 04, 2010</time>
## Forms
### Forms
= form_for @post do |f|
### Fields
f.check_box :enabled
f.text_field :title
f.text_area :body, \
:size => '60x12'
### Select dropdowns
f.time_zone_select :time_zone
f.date_select :birthday
f.select :city_id, [['Lisbon',1], ['Madrid',2], ...], 4 # (4 = selected)
f.collection_select :city_id, City.all, :id, :name
options_for_select [['Lisbon',1], ['Madrid', 2], ...], 4 # Just makes <option> tags
### The rest
f.submit "Create"
### Files
= form_for @post, :multipart => true do |f|
= f.file_field :picture