SlideShare a Scribd company logo
In
RAILS
By,
Amal Subhash
AssociateSoftwareEngineer,
PIT Solutions,
Technopark,
Trivandrum.
Date: 12-06-2015
Action View
What is Action View?
Action View and Action Controller are the two
major components of Action Pack.

In Rails, web requests are handled by Action
Pack, which splits the work into a controller
part (performing the logic) and a view part
(rendering a template).

Typically, Action Controller will be concerned
with communicating with the database and
Using Action View with Rails
For each controller there is an associated directory
in the app/views directory which holds the
template files that make up the views associated
with that controller.
These files are used to display the view that results
from each controller action.
Example of view in scaffold
generator :
$ bin/rails generate scaffold article

[...]

invoke scaffold_controller

create
app/controllers/articles_controller.r
b

invoke erb

create app/views/articles
There is a naming convention for views in
Rails. Typically, the views share their
name with the associated controller action,
as you can see above.
For example, the index controller action of
the articles_controller.rb will use the
index.html.erb view file in the
app/views/articles directory.
The complete HTML returned to the client is
composed of a combination of this ERB
file, a layout template that wraps it, and all
the partials that the view may reference
Templates, Partials and
Layouts
Templates
Action View templates can be written in
several ways. If the template file has a .erb
extension then it uses a mixture of ERB
(Embedded Ruby) and HTML. If the template
file has a .builder extension then the
Builder::XmlMarkup library is used.
ERB
Within an ERB template, Ruby code can be
included using both <% %> and <%= %>
tags. The <% %> tags are used to execute
Ruby code that does not return anything,
such as conditions, loops or blocks, and
the <%= %> tags are used when you want
output.
Consider the following loop for names:
Builder
Builder templates are a more programmatic
alternative to ERB. They are especially
useful for generating XML content. An
XmlMarkup object named xml is
automatically made available to templates
with a .builder extension.
Here are some basic examples:
xml.em("emphasized")
Partials
Partial templates - usually just called "partials" - are
another device for breaking the rendering process
into more manageable chunks. With partials, you
can extract pieces of code from your templates to
separate files and also reuse them throughout your
templates.
Naming Partials
To render a partial as part of a view, you use
the render method within the view:
<%= render "menu" %>
This will render a file named _menu.html.erb
at that point within the view that is being
rendered. Note the leading underscore
character: partials are named with a leading
underscore to distinguish them from regular
views, even though they are referred to
without the underscore. This holds true even
when you're pulling in a partial from another
Layouts
Layouts can be used to render a common view
template around the results of Rails controller
actions.
Typically, a Rails application will have a couple of
layouts that pages will be rendered within.

For example, a site might have one layout for a
logged in user and another for the marketing or
sales side of the site.
Partial Layouts
Partials can have their own layouts applied to
them. These layouts are different from those
applied to a controller action, but they work in
a similar fashion.
Let's say we're displaying an article on a page
which should be wrapped in a div for display
purposes. Firstly, we'll create a new Article:
Article.create(body: 'Partial Layouts are cool!')
In the show template, we'll render the _article
partial wrapped in the box layout:
articles/_box.html.erb
<div class='box'>
<%= yield %>
</div>
The _article partial wraps the article's body
in a div with the id of the article using the
div_for helper:
this would output the following:
<div class='box'>
<div id='article_1'>
<p>Partial Layouts are cool!</p>
</div>
</div>
Overview of helpers
provided by Action View

RecordTagHelper
This module provides methods for
generating container tags, such as div, for
your record. This is the recommended way
of creating a container for render your
Active Record object, as it adds an
appropriate class and id attributes to that
container. You can then refer to those
containers easily by following the
content_tag_for
Renders a container tag that relates to your
Active Record Object.
For example, given @article is the object of
Article class, you can do:
<%= content_tag_for(:tr, @article) do %>
This will generate this HTML output:
<tr id="article_1234" class="article">
<td>Hello World!</td>
</tr>
div_for
This is actually a convenient method which
calls content_tag_for internally with :div as
the tag name. You can pass either an
Active Record object or a collection of
objects. For example:
<%= div_for(@article, class: "frontpage") do
%>
AssetTagHelper
This module provides methods for generating
HTML that links views to assets such as
images, JavaScript files, stylesheets, and
feeds.
By default, Rails links to these assets on the
current host in the public folder, but you can
direct Rails to link to assets from a
dedicated assets server by setting
config.action_controller.asset_host in the
register_javascript_expansion
Register one or more JavaScript files to be
included when symbol is passed to
javascript_include_tag. This method is
typically intended to be called from plugin
initialization to register JavaScript files that
the plugin installed in
vendor/assets/javascripts.
ActionView::Helpers::AssetTagHelper.regist
register_stylesheet_expansion
Register one or more stylesheet files to be
included when symbol is passed to
stylesheet_link_tag. This method is typically
intended to be called from plugin
initialization to register stylesheet files that
the plugin installed in
vendor/assets/stylesheets.
ActionView::Helpers::AssetTagHelper.register
_stylesheet_expansion monkey: ["head",
auto_discovery_link_tag
Returns a link tag that browsers and feed
readers can use to auto-detect an RSS or
Atom feed.
auto_discovery_link_tag(:rss,
"http://www.example.com/feed.rss", {title:
"RSS Feed"}) # =>
<link rel="alternate"
Image_path
Computes the path to an image asset in the
app/assets/images directory. Full paths from
the document root will be passed through.
Used internally by image_tag to build the
image path.
image_path("edit.png") # => /assets/edit.png
image_url
Computes the url to an image asset in the
app/assets/images directory. This will call
image_path internally and merge with your
current host or your asset host.
image_url("edit.png") # =>
http://www.example.com/assets/edit.png
javascript_include_tag
Returns an HTML script tag for each of the
sources provided. You can pass in the
filename (.js extension is optional) of
JavaScript files that exist in your
app/assets/javascripts directory for
inclusion into the current page or you can
pass the full path relative to your document
root.
javascript_include_tag "common" # =>
<script src="/assets/common.js"></script>
javascript_path
Computes the path to a JavaScript asset in
the app/assets/javascripts directory. If the
source filename has no extension, .js will be
appended. Full paths from the document
root will be passed through. Used internally
by javascript_include_tag to build the script
path.
stylesheet_link_tag
Returns a stylesheet link tag for the sources
specified as arguments. If you don't specify
an extension, .css will be appended
automatically.
stylesheet_link_tag "application" # => <link
href="/assets/application.css"
media="screen" rel="stylesheet" />
stylesheet_path
Computes the path to a stylesheet asset in the
app/assets/stylesheets directory. If the
source filename has no extension, .css will
be appended. Full paths from the document
root will be passed through. Used internally
by stylesheet_link_tag to build the stylesheet
path.
stylesheet_path "application" # =>
/assets/application.css
DateHelper
date_select
Returns a set of select tags (one for year,
month, and day) pre-selected for
accessing a specified date-based attribute.
date_select("article", "published_on")
datetime_select
Returns a set of select tags (one for year,
month, day, hour, and minute) pre-
selected for accessing a specified
distance_of_time_in_words
Reports the approximate distance in time
between two Time or Date objects or integers
as seconds. Set include_seconds to true if
you want more detailed approximations.
distance_of_time_in_words(Time.now,
Time.now + 15.seconds) # => less than a
minute
distance_of_time_in_words(Time.now,
Time.now + 15.seconds, include_seconds:
true) # => less than 20 seconds
select_datetime
Returns a set of HTML select-tags (one for year,
month, day, hour, and minute) pre-selected
with the datetime provided.
# Generates a datetime select that defaults to the
datetime provided (four days after today)
select_datetime(Time.now + 4.days)
# Generates a datetime select that defaults to
today (no specified datetime)

select_hour

select_minute

select_month

select_second

select_time

select_year

time_ago_in_words

time_select
FormHelper
Form helpers are designed to make
working with models much easier
compared to using just standard HTML
elements by providing a set of methods for
creating forms based on your models.
This helper generates the HTML for forms,
providing a method for each sort of input
(e.g., text, password, select, and so on).
form_for, gives you the ability to create a
form for a model instance; for example,
let's say that you have a model Person
and want to create a new instance of it:
<%= form_for @person, url: {action:
"create"} do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= submit_tag 'Create' %>
<% end %>
The HTML generated for this would be:
The params object created when this form is
submitted would look like:
{"action" => "create", "controller" => "people",
"person" => {"first_name" => "William",
"last_name" => "Smith"}}
Check_box
Returns a checkbox tag tailored for
accessing a specified attribute.
file_field
Returns a file upload input tag tailored for
accessing a specified attribute.
file_field(:user, :avatar)
# => <input type="file" id="user_avatar"
name="user[avatar]" />
form_for
Creates a form and a scope around a
specific model object that is used as a base
for questioning about values for the fields.
hidden_field
Returns a hidden input tag tailored for
accessing a specified attribute.
hidden_field(:user, :token)
# => <input type="hidden" id="user_token"
name="user[token]"
value="#{@user.token}" />
label
Returns a label tag tailored for labelling an
password_field
Returns an input tag of the "password" type
tailored for accessing a specified attribute.
password_field(:login, :pass)
# => <input type="text" id="login_pass"
name="login[pass]" value="#{@login.pass}"
/>
radio_button
Returns a radio button tag for accessing a
specified attribute.
text_field
Returns an input tag of the "text" type tailored
for accessing a specified attribute.
text_field(:article, :title)
# => <input type="text" id="article_title"
name="article[title]" value="#{@article.title}"
/>
email_field
Returns an input tag of the "email" type
tailored for accessing a specified attribute.
url_field
Returns an input tag of the "url" type tailored
for accessing a specified attribute.
url_field(:user, :url)
# => <input type="url" id="user_url"
name="user[url]" value="#{@user.url}" />
FormOptionsHelper
Provides a number of methods for turning
different kinds of containers into a set of
option tags.
Example object structure for use with this
method:
class Article < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :articles
collection_radio_buttons
collection_check_boxes
country_options_for_select
country_select
option_groups_from_collection_for_select
options_for_select
options_from_collection_for_select
Select
Create a select tag and a series of contained
option tags for the provided object and
method.
Example:
select("article", "person_id", Person.all.collect
{|p| [ p.name, p.id ] }, {include_blank: true})
If @article.person_id is 1, this would become:
time_zone_options_for_select
Returns a string of option tags for pretty
much any time zone in the world.
time_zone_select
Returns select and option tags for the given
object and method, using
time_zone_options_for_select to generate
the list of option tags.
time_zone_select( "user", "time_zone")
FormTagHelper
Provides a number of methods for creating
form tags that don't rely on an Active
Record object assigned to the template like
FormHelper does. Instead, you provide the
names and values manually.
check_box_tag
Creates a check box form input tag.
field_set_tag
file_field_tag
form_tag
hidden_field_tag
image_submit_tag
label_tag
password_field_tag
radio_button_tag
select_tag
url_field_tag
date_field_tag
JavaScriptHelper
Provides functionality for working with JavaScript in
your views.
NumberHelper
Provides methods for converting numbers into
formatted strings. Methods are provided for
phone numbers, currency, percentage, precision,
positional notation, and file size.
number_to_currency
number_to_human_size
number_to_percentage
number_to_phone
number_with_delimiter
number_with_precision
Thank you

More Related Content

PDF
.NET Core, ASP.NET Core Course, Session 11
PPTX
Lightning Components Workshop
PPTX
Simple mvc4 prepared by gigin krishnan
PDF
Introduction to sails.js
PPTX
Chapter 2 lesson-2 styling the action bar
PPT
CRUD with Dojo
PDF
RubyOnRails-Cheatsheet-BlaineKendall
PPTX
Chapter 2 lesson-1 adding the action bar
.NET Core, ASP.NET Core Course, Session 11
Lightning Components Workshop
Simple mvc4 prepared by gigin krishnan
Introduction to sails.js
Chapter 2 lesson-2 styling the action bar
CRUD with Dojo
RubyOnRails-Cheatsheet-BlaineKendall
Chapter 2 lesson-1 adding the action bar

What's hot (20)

PDF
Basic Crud In Django
PPTX
Android app development lesson 1
ODP
springmvc-150923124312-lva1-app6892
DOCX
LearningMVCWithLINQToSQL
DOC
The most basic inline tag
ODP
Java Spring MVC Framework with AngularJS by Google and HTML5
PDF
.NET Core, ASP.NET Core Course, Session 13
PPTX
Drupal8 render pipeline
PPTX
Using the Features API
PPTX
Create an android app for database creation using.pptx
PDF
Spring mvc
PDF
Polymer
PDF
Lab2-android
PDF
How To Manage API Request with AXIOS on a React Native App
PDF
Create an application with ember
PDF
Core Data with Swift 3.0
PDF
.NET Core, ASP.NET Core Course, Session 12
KEY
Html 5, a gentle introduction
ODP
Lab: Developing with the extension library
KEY
Html5, a gentle introduction
Basic Crud In Django
Android app development lesson 1
springmvc-150923124312-lva1-app6892
LearningMVCWithLINQToSQL
The most basic inline tag
Java Spring MVC Framework with AngularJS by Google and HTML5
.NET Core, ASP.NET Core Course, Session 13
Drupal8 render pipeline
Using the Features API
Create an android app for database creation using.pptx
Spring mvc
Polymer
Lab2-android
How To Manage API Request with AXIOS on a React Native App
Create an application with ember
Core Data with Swift 3.0
.NET Core, ASP.NET Core Course, Session 12
Html 5, a gentle introduction
Lab: Developing with the extension library
Html5, a gentle introduction
Ad

Viewers also liked (13)

PDF
Consar 5
PDF
Parsons_resume
DOCX
PPTX
Configuration
DOCX
Guía de clase primero bloque 3
DOCX
Guía de matemática 3º básico
PPTX
Genetically Modified Foods presentation
PPTX
♪ウォーキング瞑想♪
PPTX
Verb to be
PDF
Carbohidratos
DOCX
Prueba historia 3º básico
PPT
Biotecnología alimentos
PPT
Biomedical instrumentation
Consar 5
Parsons_resume
Configuration
Guía de clase primero bloque 3
Guía de matemática 3º básico
Genetically Modified Foods presentation
♪ウォーキング瞑想♪
Verb to be
Carbohidratos
Prueba historia 3º básico
Biotecnología alimentos
Biomedical instrumentation
Ad

Similar to Actionview (20)

PPTX
Templates, partials and layouts
PDF
Survey of Front End Topics in Rails
PDF
Rails 4.0
PDF
Rupicon 2014 Action pack
PDF
RubyOnRails-Cheatsheet-BlaineKendall
PDF
RoR 101: Session 2
PDF
Ruby on rails RAD
PPT
Ruby on rails
PDF
Curso rails
PDF
Agile Web Development With Rails Third Edition Third Ruby Sam
ZIP
Rails 3 (beta) Roundup
PDF
Ruby On Rails
DOC
Rails interview questions
PPT
Ruby On Rails
KEY
Rails Antipatterns | Open Session with Chad Pytel
PPTX
Catalog display
PDF
Template rendering in rails
PDF
RoR 101: Session 6
PDF
RubyEnRails2007 - Dr Nic Williams - Keynote
ZIP
Renegades Guide to Hacking Rails Internals
Templates, partials and layouts
Survey of Front End Topics in Rails
Rails 4.0
Rupicon 2014 Action pack
RubyOnRails-Cheatsheet-BlaineKendall
RoR 101: Session 2
Ruby on rails RAD
Ruby on rails
Curso rails
Agile Web Development With Rails Third Edition Third Ruby Sam
Rails 3 (beta) Roundup
Ruby On Rails
Rails interview questions
Ruby On Rails
Rails Antipatterns | Open Session with Chad Pytel
Catalog display
Template rendering in rails
RoR 101: Session 6
RubyEnRails2007 - Dr Nic Williams - Keynote
Renegades Guide to Hacking Rails Internals

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Business Ethics Teaching Materials for college
PDF
Insiders guide to clinical Medicine.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Basic Mud Logging Guide for educational purpose
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Institutional Correction lecture only . . .
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Business Ethics Teaching Materials for college
Insiders guide to clinical Medicine.pdf
PPH.pptx obstetrics and gynecology in nursing
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
human mycosis Human fungal infections are called human mycosis..pptx
TR - Agricultural Crops Production NC III.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Basic Mud Logging Guide for educational purpose
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Institutional Correction lecture only . . .

Actionview

  • 2. What is Action View? Action View and Action Controller are the two major components of Action Pack.  In Rails, web requests are handled by Action Pack, which splits the work into a controller part (performing the logic) and a view part (rendering a template).  Typically, Action Controller will be concerned with communicating with the database and
  • 3. Using Action View with Rails For each controller there is an associated directory in the app/views directory which holds the template files that make up the views associated with that controller. These files are used to display the view that results from each controller action.
  • 4. Example of view in scaffold generator : $ bin/rails generate scaffold article  [...]  invoke scaffold_controller  create app/controllers/articles_controller.r b  invoke erb  create app/views/articles
  • 5. There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action, as you can see above. For example, the index controller action of the articles_controller.rb will use the index.html.erb view file in the app/views/articles directory. The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference
  • 6. Templates, Partials and Layouts Templates Action View templates can be written in several ways. If the template file has a .erb extension then it uses a mixture of ERB (Embedded Ruby) and HTML. If the template file has a .builder extension then the Builder::XmlMarkup library is used.
  • 7. ERB Within an ERB template, Ruby code can be included using both <% %> and <%= %> tags. The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the <%= %> tags are used when you want output. Consider the following loop for names:
  • 8. Builder Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object named xml is automatically made available to templates with a .builder extension. Here are some basic examples: xml.em("emphasized")
  • 9. Partials Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With partials, you can extract pieces of code from your templates to separate files and also reuse them throughout your templates.
  • 10. Naming Partials To render a partial as part of a view, you use the render method within the view: <%= render "menu" %> This will render a file named _menu.html.erb at that point within the view that is being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another
  • 11. Layouts Layouts can be used to render a common view template around the results of Rails controller actions. Typically, a Rails application will have a couple of layouts that pages will be rendered within.  For example, a site might have one layout for a logged in user and another for the marketing or sales side of the site.
  • 12. Partial Layouts Partials can have their own layouts applied to them. These layouts are different from those applied to a controller action, but they work in a similar fashion. Let's say we're displaying an article on a page which should be wrapped in a div for display purposes. Firstly, we'll create a new Article: Article.create(body: 'Partial Layouts are cool!') In the show template, we'll render the _article partial wrapped in the box layout:
  • 13. articles/_box.html.erb <div class='box'> <%= yield %> </div> The _article partial wraps the article's body in a div with the id of the article using the div_for helper:
  • 14. this would output the following: <div class='box'> <div id='article_1'> <p>Partial Layouts are cool!</p> </div> </div>
  • 15. Overview of helpers provided by Action View  RecordTagHelper This module provides methods for generating container tags, such as div, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the
  • 16. content_tag_for Renders a container tag that relates to your Active Record Object. For example, given @article is the object of Article class, you can do: <%= content_tag_for(:tr, @article) do %>
  • 17. This will generate this HTML output: <tr id="article_1234" class="article"> <td>Hello World!</td> </tr>
  • 18. div_for This is actually a convenient method which calls content_tag_for internally with :div as the tag name. You can pass either an Active Record object or a collection of objects. For example: <%= div_for(@article, class: "frontpage") do %>
  • 19. AssetTagHelper This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds. By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting config.action_controller.asset_host in the
  • 20. register_javascript_expansion Register one or more JavaScript files to be included when symbol is passed to javascript_include_tag. This method is typically intended to be called from plugin initialization to register JavaScript files that the plugin installed in vendor/assets/javascripts. ActionView::Helpers::AssetTagHelper.regist
  • 21. register_stylesheet_expansion Register one or more stylesheet files to be included when symbol is passed to stylesheet_link_tag. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in vendor/assets/stylesheets. ActionView::Helpers::AssetTagHelper.register _stylesheet_expansion monkey: ["head",
  • 22. auto_discovery_link_tag Returns a link tag that browsers and feed readers can use to auto-detect an RSS or Atom feed. auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "RSS Feed"}) # => <link rel="alternate"
  • 23. Image_path Computes the path to an image asset in the app/assets/images directory. Full paths from the document root will be passed through. Used internally by image_tag to build the image path. image_path("edit.png") # => /assets/edit.png
  • 24. image_url Computes the url to an image asset in the app/assets/images directory. This will call image_path internally and merge with your current host or your asset host. image_url("edit.png") # => http://www.example.com/assets/edit.png
  • 25. javascript_include_tag Returns an HTML script tag for each of the sources provided. You can pass in the filename (.js extension is optional) of JavaScript files that exist in your app/assets/javascripts directory for inclusion into the current page or you can pass the full path relative to your document root. javascript_include_tag "common" # => <script src="/assets/common.js"></script>
  • 26. javascript_path Computes the path to a JavaScript asset in the app/assets/javascripts directory. If the source filename has no extension, .js will be appended. Full paths from the document root will be passed through. Used internally by javascript_include_tag to build the script path.
  • 27. stylesheet_link_tag Returns a stylesheet link tag for the sources specified as arguments. If you don't specify an extension, .css will be appended automatically. stylesheet_link_tag "application" # => <link href="/assets/application.css" media="screen" rel="stylesheet" />
  • 28. stylesheet_path Computes the path to a stylesheet asset in the app/assets/stylesheets directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path. stylesheet_path "application" # => /assets/application.css
  • 29. DateHelper date_select Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute. date_select("article", "published_on") datetime_select Returns a set of select tags (one for year, month, day, hour, and minute) pre- selected for accessing a specified
  • 30. distance_of_time_in_words Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set include_seconds to true if you want more detailed approximations. distance_of_time_in_words(Time.now, Time.now + 15.seconds) # => less than a minute distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true) # => less than 20 seconds
  • 31. select_datetime Returns a set of HTML select-tags (one for year, month, day, hour, and minute) pre-selected with the datetime provided. # Generates a datetime select that defaults to the datetime provided (four days after today) select_datetime(Time.now + 4.days) # Generates a datetime select that defaults to today (no specified datetime)
  • 33. FormHelper Form helpers are designed to make working with models much easier compared to using just standard HTML elements by providing a set of methods for creating forms based on your models. This helper generates the HTML for forms, providing a method for each sort of input (e.g., text, password, select, and so on).
  • 34. form_for, gives you the ability to create a form for a model instance; for example, let's say that you have a model Person and want to create a new instance of it: <%= form_for @person, url: {action: "create"} do |f| %> <%= f.text_field :first_name %> <%= f.text_field :last_name %> <%= submit_tag 'Create' %> <% end %> The HTML generated for this would be:
  • 35. The params object created when this form is submitted would look like: {"action" => "create", "controller" => "people", "person" => {"first_name" => "William", "last_name" => "Smith"}} Check_box Returns a checkbox tag tailored for accessing a specified attribute.
  • 36. file_field Returns a file upload input tag tailored for accessing a specified attribute. file_field(:user, :avatar) # => <input type="file" id="user_avatar" name="user[avatar]" /> form_for Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
  • 37. hidden_field Returns a hidden input tag tailored for accessing a specified attribute. hidden_field(:user, :token) # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" /> label Returns a label tag tailored for labelling an
  • 38. password_field Returns an input tag of the "password" type tailored for accessing a specified attribute. password_field(:login, :pass) # => <input type="text" id="login_pass" name="login[pass]" value="#{@login.pass}" /> radio_button Returns a radio button tag for accessing a specified attribute.
  • 39. text_field Returns an input tag of the "text" type tailored for accessing a specified attribute. text_field(:article, :title) # => <input type="text" id="article_title" name="article[title]" value="#{@article.title}" /> email_field Returns an input tag of the "email" type tailored for accessing a specified attribute.
  • 40. url_field Returns an input tag of the "url" type tailored for accessing a specified attribute. url_field(:user, :url) # => <input type="url" id="user_url" name="user[url]" value="#{@user.url}" /> FormOptionsHelper Provides a number of methods for turning different kinds of containers into a set of option tags.
  • 41. Example object structure for use with this method: class Article < ActiveRecord::Base belongs_to :author end class Author < ActiveRecord::Base has_many :articles
  • 43. Select Create a select tag and a series of contained option tags for the provided object and method. Example: select("article", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: true}) If @article.person_id is 1, this would become:
  • 44. time_zone_options_for_select Returns a string of option tags for pretty much any time zone in the world. time_zone_select Returns select and option tags for the given object and method, using time_zone_options_for_select to generate the list of option tags. time_zone_select( "user", "time_zone")
  • 45. FormTagHelper Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually. check_box_tag Creates a check box form input tag.
  • 48. JavaScriptHelper Provides functionality for working with JavaScript in your views. NumberHelper Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.