2013-10-14 02:36:58 +00:00
|
|
|
---
|
2023-03-14 04:40:29 +00:00
|
|
|
title: Rails helpers
|
2015-11-24 05:02:17 +00:00
|
|
|
category: Rails
|
2012-10-11 08:13:19 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
### Date
|
|
|
|
|
|
|
|
distance_of_time_in_words(Time.now, project.end_date) #=> 3 hours
|
2014-06-04 07:14:41 +00:00
|
|
|
distance_of_time_in_words_to_now(project.end_date) #=> 3 hours
|
|
|
|
|
|
|
|
time_ago_in_words 3.minutes.ago #=> "3 minutes"
|
|
|
|
|
|
|
|
### Numbers
|
|
|
|
|
|
|
|
number_to_currency 20.33
|
2014-06-26 08:43:36 +00:00
|
|
|
number_to_currency 20.33, precision: 0
|
2014-06-04 07:14:41 +00:00
|
|
|
number_with_precision 3.14159, precision: 2
|
|
|
|
number_to_percentage 32 #=> "32%"
|
|
|
|
number_with_delimiter 2048 #=> "2,048"
|
|
|
|
number_to_human 12000000 #=> "12 million"
|
|
|
|
number_to_human_size 12000000 #=> "12 MB"
|
|
|
|
number_to_phone "5551234" #=> "555-1234"
|
|
|
|
|
|
|
|
### Cache
|
|
|
|
|
|
|
|
<% cache project do %>
|
|
|
|
<% cache [project, current_user] do %>
|
|
|
|
|
|
|
|
<% cache_if admin?, project do %>
|
|
|
|
<% cache_unless admin?, project do %>
|
|
|
|
|
|
|
|
### Tags
|
|
|
|
|
|
|
|
tag("br")
|
|
|
|
tag("img", src: "image.jpg")
|
|
|
|
content_tag(:p, "Hello")
|
2012-10-11 08:13:19 +00:00
|
|
|
|
|
|
|
### 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", \
|
2014-06-04 07:14:41 +00:00
|
|
|
include_seconds: true,
|
|
|
|
minute_step: 15,
|
|
|
|
prompt: true,
|
|
|
|
prompt: { hour: "Choose hr", minute: "Choose min", second: "Choose sec" },
|
|
|
|
ampm: true
|
2012-10-11 08:13:19 +00:00
|
|
|
|
|
|
|
# For dates (all options are optional)
|
|
|
|
date_select "article", "written_on", \
|
2014-06-04 07:14:41 +00:00
|
|
|
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' }
|
2012-10-11 08:13:19 +00:00
|
|
|
|
|
|
|
### Time tag
|
|
|
|
|
2014-06-04 07:14:41 +00:00
|
|
|
time_tag Date.today
|
|
|
|
#=> '<time datetime="2010-11-04">November 04, 2010<%rtime>'
|
2012-10-11 08:13:19 +00:00
|
|
|
|
2014-06-04 07:14:41 +00:00
|
|
|
time_tag Time.now
|
|
|
|
#=> '<time datetime="2010-11-04T17:55:45+01:00">November 04, 2010 17:55</time>'
|
2012-10-11 08:13:19 +00:00
|
|
|
|
2014-06-04 07:14:41 +00:00
|
|
|
time_tag Date.yesterday, 'Yesterday'
|
|
|
|
#=> '<time datetime="2010-11-03">Yesterday<%rtime>'
|
2013-03-25 17:44:24 +00:00
|
|
|
|
2014-06-04 07:14:41 +00:00
|
|
|
time_tag Date.today, pubdate: true
|
|
|
|
#=> '<time datetime="2010-11-04" pubdate="pubdate">November 04, 2010</time>'
|
|
|
|
|
|
|
|
time_tag Date.today, \
|
|
|
|
format: :short_date # (en.time.formats.short_date)
|
2013-03-25 17:44:24 +00:00
|
|
|
|
2012-10-11 08:13:19 +00:00
|
|
|
### Files
|
|
|
|
|
2014-06-04 07:14:41 +00:00
|
|
|
= form_for @post, multipart: true do |f|
|
2012-10-11 08:13:19 +00:00
|
|
|
= f.file_field :picture
|
2013-08-24 07:42:48 +00:00
|
|
|
|
|
|
|
### i18n
|
|
|
|
|
|
|
|
t('folders')
|
|
|
|
t('folders.save')
|
|
|
|
|
|
|
|
l(Time.now)
|
|
|
|
|
|
|
|
t('x_files', count: files.count)
|
|
|
|
# files:
|
|
|
|
# one: 'one file'
|
|
|
|
# other: '%{count} files'
|
2014-06-04 07:14:41 +00:00
|
|
|
|
|
|
|
### References
|
|
|
|
|
|
|
|
* http://api.rubyonrails.org/classes/ActionView/Helpers.html
|