2018-07-23 22:29:31 -04:00
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.**
2014-12-23 17:32:50 -05:00
2012-09-01 17:25:58 -04:00
Action View Overview
====================
2009-04-05 19:29:03 -04:00
2012-11-29 17:25:02 -05:00
After reading this guide, you will know:
2009-04-05 19:29:03 -04:00
2012-11-29 08:14:08 -05:00
* What Action View is and how to use it with Rails.
* How best to use templates, partials, and layouts.
2017-08-30 22:21:13 -04:00
* What helpers are provided by Action View.
2012-11-29 08:14:08 -05:00
* How to use localized views.
2009-04-05 19:29:03 -04:00
2012-09-01 17:25:58 -04:00
--------------------------------------------------------------------------------
2009-04-05 19:29:03 -04:00
2012-09-01 17:25:58 -04:00
What is Action View?
--------------------
2009-04-05 19:29:03 -04:00
2016-05-31 10:47:34 -04:00
In Rails, web requests are handled by [Action Controller ](action_controller_overview.html ) and Action View. Typically, Action Controller is concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the response.
2009-09-03 16:55:55 -04:00
2010-08-14 01:13:00 -04:00
Action View templates are written using embedded Ruby in tags mingled with HTML. To avoid cluttering the templates with boilerplate code, a number of helper classes provide common behavior for forms, dates, and strings. It's also easy to add new helpers to your application as it evolves.
2009-09-03 16:55:55 -04:00
2012-11-15 13:14:10 -05:00
NOTE: Some features of Action View are tied to Active Record, but that doesn't mean Action View depends on Active Record. Action View is an independent package that can be used with any sort of Ruby libraries.
2009-04-05 19:29:03 -04:00
2012-09-01 17:25:58 -04:00
Using Action View with Rails
----------------------------
2009-04-05 19:29:03 -04:00
2012-09-01 19:34:21 -04:00
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.
2011-04-16 15:02:05 -04:00
Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
2012-09-01 20:45:26 -04:00
```bash
2018-06-26 16:02:51 -04:00
$ rails generate scaffold article
2011-04-16 15:02:05 -04:00
[...]
invoke scaffold_controller
2014-05-21 21:47:18 -04:00
create app/controllers/articles_controller.rb
2011-04-16 15:02:05 -04:00
invoke erb
2014-05-21 21:47:18 -04:00
create app/views/articles
create app/views/articles/index.html.erb
create app/views/articles/edit.html.erb
create app/views/articles/show.html.erb
create app/views/articles/new.html.erb
create app/views/articles/_form.html.erb
2011-04-16 15:02:05 -04:00
[...]
2012-09-01 17:08:06 -04:00
```
2011-04-16 15:02:05 -04:00
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.
2014-05-21 21:47:18 -04:00
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.
2014-07-23 19:16:03 -04:00
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. Within this guide you will find more detailed documentation about each of these three components.
2009-04-05 19:29:03 -04:00
2009-09-03 18:51:11 -04:00
2018-05-08 22:48:07 -04:00
Templates, Partials, and Layouts
2012-09-01 17:25:58 -04:00
-------------------------------
2009-04-05 19:29:03 -04:00
2014-07-23 19:16:03 -04:00
As mentioned, the final HTML output is a composition of three Rails elements: `Templates` , `Partials` and `Layouts` .
Below is a brief overview of each of them.
2011-04-16 16:14:46 -04:00
2012-09-01 17:25:58 -04:00
### Templates
2011-04-16 17:26:44 -04:00
2014-07-23 19:16:03 -04:00
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.
2011-04-16 17:26:44 -04:00
2012-09-01 19:34:21 -04:00
Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an HTML file using the ERB template system will have `.html.erb` as a file extension.
2011-04-16 17:26:44 -04:00
2012-09-01 17:25:58 -04:00
#### ERB
2011-04-16 17:26:44 -04:00
2018-05-08 22:48:07 -04:00
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.
2011-04-16 17:26:44 -04:00
Consider the following loop for names:
2012-09-03 21:21:24 -04:00
```html+erb
2012-11-15 12:08:32 -05:00
< h1 > Names of all the people< / h1 >
2011-04-16 17:26:44 -04:00
< % @people .each do |person| %>
2013-08-07 16:08:53 -04:00
Name: < %= person.name %>< br >
2011-04-16 17:26:44 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
2014-07-23 19:16:03 -04:00
The loop is set up using regular embedding tags (`< % %>`) and the name is inserted using the output embedding tags (`< %= %>`). Note that this is not just a usage suggestion: regular output functions such as `print` and `puts` won't be rendered to the view with ERB templates. So this would be wrong:
2011-04-16 17:26:44 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2011-04-16 17:26:44 -04:00
< %# WRONG %>
Hi, Mr. < % puts "Frodo" %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
2012-11-16 14:43:52 -05:00
To suppress leading and trailing whitespaces, you can use `<%-` `-%>` interchangeably with `<%` and `%>` .
2011-04-16 17:26:44 -04:00
2012-09-01 17:25:58 -04:00
#### Builder
2011-04-16 17:26:44 -04:00
2012-09-01 21:37:59 -04:00
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.
2011-04-16 17:26:44 -04:00
Here are some basic examples:
2012-09-01 17:08:06 -04:00
```ruby
2011-04-16 17:26:44 -04:00
xml.em("emphasized")
xml.em { xml.b("emph & bold") }
2012-11-15 15:43:23 -05:00
xml.a("A Link", "href" => "http://rubyonrails.org")
xml.target("name" => "compile", "option" => "fast")
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
2012-11-15 12:51:50 -05:00
which would produce:
2011-04-16 17:26:44 -04:00
2012-09-01 17:08:06 -04:00
```html
2011-04-16 17:26:44 -04:00
< em > emphasized< / em >
< em > < b > emph & bold< / b > < / em >
< a href = "http://rubyonrails.org" > A link< / a >
2011-06-10 07:36:21 -04:00
< target option = "fast" name = "compile" / >
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
Any method with a block will be treated as an XML markup tag with nested markup in the block. For example, the following:
2012-09-01 17:08:06 -04:00
```ruby
2011-04-16 17:26:44 -04:00
xml.div {
xml.h1(@person.name)
xml.p(@person.bio)
}
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
would produce something like:
2012-09-01 17:08:06 -04:00
```html
2011-04-16 17:26:44 -04:00
< div >
< h1 > David Heinemeier Hansson< / h1 >
< p > A product of Danish Design during the Winter of '79...< / p >
< / div >
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
2012-11-15 12:51:50 -05:00
Below is a full-length RSS example actually used on Basecamp:
2011-04-16 17:26:44 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-04-16 17:26:44 -04:00
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title(@feed_title)
xml.link(@url)
xml.description "Basecamp: Recent items"
xml.language "en-us"
xml.ttl "40"
for item in @recent_items
xml.item do
xml.title(item_title(item))
xml.description(item_description(item)) if item_description(item)
xml.pubDate(item_pubDate(item))
xml.guid(@person.firm.account.url + @recent_items .url(item))
xml.link(@person.firm.account.url + @recent_items .url(item))
xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
end
end
end
end
2012-09-01 17:08:06 -04:00
```
2011-04-16 17:26:44 -04:00
2015-09-23 15:57:52 -04:00
#### Jbuilder
2015-09-24 02:36:42 -04:00
[Jbuilder ](https://github.com/rails/jbuilder ) is a gem that's
2017-11-30 01:25:50 -05:00
maintained by the Rails team and included in the default Rails `Gemfile` .
2015-09-23 15:57:52 -04:00
It's similar to Builder, but is used to generate JSON, instead of XML.
2017-11-30 01:25:50 -05:00
If you don't have it, you can add the following to your `Gemfile` :
2015-09-23 15:57:52 -04:00
```ruby
gem 'jbuilder'
```
A Jbuilder object named `json` is automatically made available to templates with
a `.jbuilder` extension.
Here is a basic example:
```ruby
json.name("Alex")
json.email("alex@example.com")
```
would produce:
```json
{
"name": "Alex",
2016-02-27 01:25:35 -05:00
"email": "alex@example.com"
2015-09-23 15:57:52 -04:00
}
```
2016-03-29 09:28:10 -04:00
See the [Jbuilder documentation ](https://github.com/rails/jbuilder#jbuilder ) for
2015-09-24 02:36:42 -04:00
more examples and information.
2015-09-23 15:57:52 -04:00
2012-09-01 17:25:58 -04:00
#### Template Caching
2011-04-16 17:26:44 -04:00
By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will check the file's modification time and recompile it in development mode.
2012-09-01 17:25:58 -04:00
### Partials
2011-04-16 16:14:46 -04:00
2013-08-23 11:59:11 -04:00
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.
2011-04-16 16:14:46 -04:00
2012-09-01 17:25:58 -04:00
#### Naming Partials
2011-04-16 16:14:46 -04:00
2012-09-01 21:37:59 -04:00
To render a partial as part of a view, you use the `render` method within the view:
2011-04-16 16:14:46 -04:00
2012-09-03 21:21:24 -04:00
```erb
2011-04-16 16:14:46 -04:00
< %= render "menu" %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2012-11-15 12:51:50 -05:00
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 folder:
2011-04-16 16:14:46 -04:00
2012-09-03 21:21:24 -04:00
```erb
2011-04-16 16:14:46 -04:00
< %= render "shared/menu" %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2012-09-01 21:37:59 -04:00
That code will pull in the partial from `app/views/shared/_menu.html.erb` .
2011-04-16 16:14:46 -04:00
2012-09-01 17:25:58 -04:00
#### Using Partials to simplify Views
2011-04-16 16:14:46 -04:00
2013-04-04 13:14:48 -04:00
One way to use partials is to treat them as the equivalent of subroutines; a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looks like this:
2011-04-16 16:14:46 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2011-04-16 16:14:46 -04:00
< %= render "shared/ad_banner" %>
< h1 > Products< / h1 >
< p > Here are a few of our fine products:< / p >
< % @products .each do |product| %>
2015-03-22 07:15:36 -04:00
< %= render partial: "product", locals: { product: product } %>
2011-04-16 16:14:46 -04:00
< % end %>
< %= render "shared/footer" %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2012-09-01 21:37:59 -04:00
Here, the `_ad_banner.html.erb` and `_footer.html.erb` partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
2011-04-16 16:14:46 -04:00
2015-02-08 18:38:20 -05:00
#### `render` without `partial` and `locals` options
2015-02-15 06:30:38 -05:00
In the above example, `render` takes 2 options: `partial` and `locals` . But if
these are the only options you want to pass, you can skip using these options.
For example, instead of:
2015-02-08 18:38:20 -05:00
```erb
2015-03-22 07:15:36 -04:00
< %= render partial: "product", locals: { product: @product } %>
2015-02-08 18:38:20 -05:00
```
You can also do:
```erb
< %= render "product", product: @product %>
```
2012-11-15 15:43:23 -05:00
#### The `as` and `object` options
2011-04-16 16:14:46 -04:00
2012-11-15 15:43:23 -05:00
By default `ActionView::Partials::PartialRenderer` has its object in a local variable with the same name as the template. So, given:
2011-04-16 16:14:46 -04:00
2012-09-01 17:08:06 -04:00
```erb
2012-11-15 15:43:23 -05:00
< %= render partial: "product" %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2015-06-07 06:05:46 -04:00
within `_product` partial we'll get `@product` in the local variable `product` ,
as if we had written:
2011-04-16 16:14:46 -04:00
2012-09-01 17:08:06 -04:00
```erb
2015-03-22 07:15:36 -04:00
< %= render partial: "product", locals: { product: @product } %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2016-02-24 08:05:24 -05:00
The `object` option can be used to directly specify which object is rendered into the partial; useful when the template's object is elsewhere (e.g. in a different instance variable or in a local variable).
2011-04-16 16:14:46 -04:00
For example, instead of:
2012-09-01 17:08:06 -04:00
```erb
2015-03-22 07:15:36 -04:00
< %= render partial: "product", locals: { product: @item } %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2012-11-15 12:51:50 -05:00
we would do:
2011-04-16 16:14:46 -04:00
2012-09-01 17:08:06 -04:00
```erb
2012-11-15 15:43:23 -05:00
< %= render partial: "product", object: @item %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2016-09-13 03:21:33 -04:00
With the `as` option we can specify a different name for the said local variable. For example, if we wanted it to be `item` instead of `product` we would do:
2012-11-15 12:51:50 -05:00
```erb
2012-11-15 15:43:23 -05:00
< %= render partial: "product", object: @item , as: "item" %>
2012-11-15 12:51:50 -05:00
```
2011-04-16 16:14:46 -04:00
2016-09-13 03:21:33 -04:00
This is equivalent to
```erb
< %= render partial: "product", locals: { item: @item } %>
```
2012-09-01 17:25:58 -04:00
#### Rendering Collections
2011-04-16 16:14:46 -04:00
2014-07-23 19:16:03 -04:00
It is very common that a template will need to iterate over a collection and render a sub-template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial for each one of the elements in the array.
2012-11-15 13:14:10 -05:00
So this example for rendering all the products:
```erb
< % @products .each do |product| %>
2012-11-15 15:43:23 -05:00
< %= render partial: "product", locals: { product: product } %>
2012-11-15 13:14:10 -05:00
< % end %>
```
can be rewritten in a single line:
2011-04-16 16:14:46 -04:00
2012-09-01 17:08:06 -04:00
```erb
2012-11-15 15:43:23 -05:00
< %= render partial: "product", collection: @products %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:14:46 -04:00
2014-07-23 19:16:03 -04:00
When a partial is called with a collection, the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is `_product` , and within it you can refer to `product` to get the collection member that is being rendered.
2011-04-16 16:14:46 -04:00
2012-11-15 13:14:10 -05:00
You can use a shorthand syntax for rendering collections. Assuming `@products` is a collection of `Product` instances, you can simply write the following to produce the same result:
2011-04-16 16:14:46 -04:00
2012-09-01 17:08:06 -04:00
```erb
2011-04-16 16:14:46 -04:00
< %= render @products %>
2012-09-01 17:08:06 -04:00
```
2009-04-05 19:29:03 -04:00
2014-07-23 19:16:03 -04:00
Rails determines the name of the partial to use by looking at the model name in the collection, `Product` in this case. In fact, you can even render a collection made up of instances of different models using this shorthand, and Rails will choose the proper partial for each member of the collection.
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### Spacer Templates
2011-04-16 16:32:04 -04:00
2012-09-01 21:37:59 -04:00
You can also specify a second partial to be rendered between instances of the main partial by using the `:spacer_template` option:
2011-04-16 16:32:04 -04:00
2012-09-01 17:08:06 -04:00
```erb
2013-11-30 21:31:15 -05:00
< %= render partial: @products , spacer_template: "product_ruler" %>
2012-09-01 17:08:06 -04:00
```
2011-04-16 16:32:04 -04:00
2012-11-15 13:14:10 -05:00
Rails will render the `_product_ruler` partial (with no data passed to it) between each pair of `_product` partials.
2011-04-16 16:32:04 -04:00
2012-09-01 17:25:58 -04:00
### Layouts
2011-04-16 17:26:44 -04:00
2014-07-23 19:16:03 -04:00
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. The logged in user layout might include top-level navigation that should be present across many controller actions. The sales layout for a SaaS app might include top-level navigation for things like "Pricing" and "Contact Us" pages. You would expect each layout to have a different look and feel. You can read about layouts in more detail in the [Layouts and Rendering in Rails ](layouts_and_rendering.html ) guide.
2009-04-05 19:29:03 -04:00
2012-09-01 17:25:58 -04:00
Partial Layouts
---------------
2009-04-05 19:29:03 -04:00
2014-07-23 19:16:03 -04:00
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.
2009-09-03 18:51:11 -04:00
2014-07-23 19:16:03 -04:00
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` :
2009-09-03 18:51:11 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
Article.create(body: 'Partial Layouts are cool!')
2012-09-01 17:08:06 -04:00
```
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
In the `show` template, we'll render the `_article` partial wrapped in the `box` layout:
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
**articles/show.html.erb**
2009-09-03 18:51:11 -04:00
2012-09-03 21:21:24 -04:00
```erb
2015-03-22 07:15:36 -04:00
< %= render partial: 'article', layout: 'box', locals: { article: @article } %>
2012-09-01 17:08:06 -04:00
```
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
The `box` layout simply wraps the `_article` partial in a `div` :
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
**articles/_box.html.erb**
2009-09-03 18:51:11 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-03 18:51:11 -04:00
< div class = 'box' >
< %= yield %>
< / div >
2012-09-01 17:08:06 -04:00
```
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
Note that the partial layout has access to the local `article` variable that was passed into the `render` call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
You can also render a block of code within a partial layout instead of calling `yield` . For example, if we didn't have the `_article` partial, we could do this instead:
2009-09-03 18:51:11 -04:00
2014-05-21 21:47:18 -04:00
**articles/show.html.erb**
2009-09-03 18:51:11 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2015-03-22 07:15:36 -04:00
< % render(layout: 'box', locals: { article: @article }) do %>
2015-05-23 08:17:20 -04:00
< div >
2014-05-21 21:47:18 -04:00
< p > < %= article.body %>< / p >
2015-05-23 08:17:20 -04:00
< / div >
2009-09-03 18:51:11 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-03 18:51:11 -04:00
2012-11-15 13:14:10 -05:00
Supposing we use the same `_box` partial from above, this would produce the same output as the previous example.
2009-04-05 19:29:03 -04:00
2012-09-01 17:25:58 -04:00
View Paths
----------
2009-04-05 19:29:03 -04:00
2015-04-25 07:49:35 -04:00
When rendering a response, the controller needs to resolve where the different
views are located. By default it only looks inside the `app/views` directory.
2015-04-12 17:43:25 -04:00
2015-04-25 07:49:35 -04:00
We can add other locations and give them a certain precedence when resolving
paths using the `prepend_view_path` and `append_view_path` methods.
2015-04-06 17:13:49 -04:00
### Prepend view path
2015-04-25 07:49:35 -04:00
This can be helpful for example, when we want to put views inside a different
directory for subdomains.
2015-04-12 17:43:25 -04:00
We can do this by using:
2015-04-06 17:13:49 -04:00
2015-04-25 07:49:35 -04:00
```ruby
prepend_view_path "app/views/#{request.subdomain}"
2015-04-12 17:43:25 -04:00
```
2015-04-25 07:49:35 -04:00
Then Action View will look first in this directory when resolving views.
2015-04-06 17:13:49 -04:00
### Append view path
2015-04-12 17:43:25 -04:00
Similarly, we can append paths:
2015-04-06 17:13:49 -04:00
2015-04-25 07:49:35 -04:00
```ruby
append_view_path "app/views/direct"
```
2015-04-06 17:13:49 -04:00
2015-04-25 07:49:35 -04:00
This will add `app/views/direct` to the end of the lookup paths.
2009-04-05 19:29:03 -04:00
2012-11-16 14:43:52 -05:00
Overview of helpers provided by Action View
-------------------------------------------
2009-04-05 19:29:03 -04:00
2012-11-15 20:32:40 -05:00
WIP: Not all the helpers are listed here. For a full list see the [API documentation ](http://api.rubyonrails.org/classes/ActionView/Helpers.html )
2012-11-15 13:30:23 -05:00
The following is only a brief overview summary of the helpers available in Action View. It's recommended that you review the [API Documentation ](http://api.rubyonrails.org/classes/ActionView/Helpers.html ), which covers all of the helpers in more detail, but this should serve as a good starting point.
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
### AssetTagHelper
2009-09-03 23:35:18 -04:00
2010-12-23 13:47:44 -05:00
This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds.
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
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 application configuration, typically in `config/environments/production.rb` . For example, let's say your asset host is `assets.example.com` :
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-02-28 21:50:59 -05:00
config.action_controller.asset_host = "assets.example.com"
2017-08-16 14:34:02 -04:00
image_tag("rails.png") # => < img src = "http://assets.example.com/images/rails.png" / >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### auto_discovery_link_tag
2009-09-03 23:35:18 -04:00
2017-05-20 05:00:13 -04:00
Returns a link tag that browsers and feed readers can use to auto-detect an RSS, Atom, or JSON feed.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-03-22 07:15:36 -04:00
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", { title: "RSS Feed" }) # =>
2015-04-03 18:37:23 -04:00
< link rel = "alternate" type = "application/rss+xml" title = "RSS Feed" href = "http://www.example.com/feed.rss" / >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### image_path
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
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.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
image_path("edit.png") # => /assets/edit.png
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-05-25 13:25:59 -04:00
Fingerprint will be added to the filename if config.assets.digest is set to true.
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
image_path("edit.png") # => /assets/edit-2d1a2db63fc738690021fedb5a65b68e.png
2012-09-01 17:08:06 -04:00
```
2012-05-25 13:25:59 -04:00
2012-09-01 17:25:58 -04:00
#### image_url
2012-02-03 18:10:37 -05:00
2016-02-24 08:05:24 -05:00
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.
2012-02-03 18:10:37 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
image_url("edit.png") # => http://www.example.com/assets/edit.png
2012-09-01 17:08:06 -04:00
```
2012-02-03 18:10:37 -05:00
2012-09-01 17:25:58 -04:00
#### image_tag
2009-09-03 23:35:18 -04:00
2014-08-08 17:25:18 -04:00
Returns an HTML image tag for the source. The source can be a full path or a file that exists in your `app/assets/images` directory.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2017-08-16 14:34:02 -04:00
image_tag("icon.png") # => < img src = "/assets/icon.png" / >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### javascript_include_tag
2009-09-03 23:35:18 -04:00
2014-08-08 17:25:18 -04:00
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.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
javascript_include_tag "common" # => < script src = "/assets/common.js" > < / script >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### javascript_path
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
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.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
javascript_path "common" # => /assets/common.js
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### javascript_url
2012-02-03 18:10:37 -05:00
2016-02-24 08:05:24 -05:00
Computes the URL to a JavaScript asset in the `app/assets/javascripts` directory. This will call `javascript_path` internally and merge with your current host or your asset host.
2012-02-03 18:10:37 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
javascript_url "common" # => http://www.example.com/assets/common.js
2012-09-01 17:08:06 -04:00
```
2012-02-03 18:10:37 -05:00
2012-09-01 17:25:58 -04:00
#### stylesheet_link_tag
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
Returns a stylesheet link tag for the sources specified as arguments. If you don't specify an extension, `.css` will be appended automatically.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-27 10:31:55 -04:00
stylesheet_link_tag "application" # => < link href = "/assets/application.css" media = "screen" rel = "stylesheet" / >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### stylesheet_path
2009-09-03 23:35:18 -04:00
2017-10-06 02:02:56 -04:00
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.
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-25 13:25:59 -04:00
stylesheet_path "application" # => /assets/application.css
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### stylesheet_url
2012-02-03 18:10:37 -05:00
2016-02-24 08:05:24 -05:00
Computes the URL to a stylesheet asset in the `app/assets/stylesheets` directory. This will call `stylesheet_path` internally and merge with your current host or your asset host.
2012-02-03 18:10:37 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-25 13:25:59 -04:00
stylesheet_url "application" # => http://www.example.com/assets/application.css
2012-09-01 17:08:06 -04:00
```
2012-02-03 18:10:37 -05:00
2012-09-01 17:25:58 -04:00
### AtomFeedHelper
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### atom_feed
2009-09-03 23:35:18 -04:00
2012-02-05 10:16:03 -05:00
This helper makes building an Atom feed easy. Here's a full usage example:
2009-09-03 23:35:18 -04:00
2012-09-06 22:26:59 -04:00
**config/routes.rb**
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
resources :articles
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2014-05-21 21:47:18 -04:00
**app/controllers/articles_controller.rb**
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-03 23:35:18 -04:00
def index
2014-05-21 21:47:18 -04:00
@articles = Article.all
2009-09-03 23:35:18 -04:00
respond_to do |format|
format.html
format.atom
end
end
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2014-05-21 21:47:18 -04:00
**app/views/articles/index.atom.builder**
2009-09-03 23:35:18 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-03 23:35:18 -04:00
atom_feed do |feed|
2014-05-21 21:47:18 -04:00
feed.title("Articles Index")
2015-05-07 14:24:23 -04:00
feed.updated(@articles.first.created_at)
2009-09-03 23:35:18 -04:00
2014-05-21 21:47:18 -04:00
@articles .each do |article|
feed.entry(article) do |entry|
entry.title(article.title)
entry.content(article.body, type: 'html')
2009-09-03 23:35:18 -04:00
entry.author do |author|
2014-05-21 21:47:18 -04:00
author.name(article.author_name)
2009-09-03 23:35:18 -04:00
end
end
end
end
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
### BenchmarkHelper
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### benchmark
2009-09-03 23:35:18 -04:00
Allows you to measure the execution time of a block in a template and records the result to the log. Wrap this block around expensive operations or possible bottlenecks to get a time reading for the operation.
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-03 23:35:18 -04:00
< % benchmark "Process data files" do %>
< %= expensive_files_operation %>
2010-08-14 01:13:00 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
This would add something like "Process data files (0.34523)" to the log, which you can then use to compare timings when optimizing your code.
2012-09-01 17:25:58 -04:00
### CacheHelper
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### cache
2009-09-03 23:35:18 -04:00
2016-02-24 11:53:38 -05:00
A method for caching fragments of a view rather than an entire action or page. This technique is useful for caching pieces like menus, lists of news topics, static HTML fragments, and so on. This method takes a block that contains the content you wish to cache. See `AbstractController::Caching::Fragments` for more information.
2009-09-03 23:35:18 -04:00
2012-09-03 21:21:24 -04:00
```erb
2009-09-03 23:35:18 -04:00
< % cache do %>
2010-08-26 15:27:44 -04:00
< %= render "shared/footer" %>
2009-09-03 23:35:18 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
### CaptureHelper
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### capture
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
The `capture` method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
2009-09-03 23:35:18 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-03 23:35:18 -04:00
< % @greeting = capture do %>
< p > Welcome! The date and time is < %= Time.now %>< / p >
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
The captured variable can then be used anywhere else.
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-03 23:35:18 -04:00
< html >
< head >
< title > Welcome!< / title >
< / head >
< body >
< %= @greeting %>
< / body >
< / html >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
#### content_for
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
Calling `content_for` stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to `yield` .
2009-09-03 23:35:18 -04:00
2012-09-01 21:37:59 -04:00
For example, let's say we have a standard application layout, but also a special page that requires certain JavaScript that the rest of the site doesn't need. We can use `content_for` to include this JavaScript on our special page without fattening up the rest of the site.
2009-09-03 23:35:18 -04:00
2012-09-06 22:26:59 -04:00
**app/views/layouts/application.html.erb**
2009-09-03 23:35:18 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-03 23:35:18 -04:00
< html >
< head >
< title > Welcome!< / title >
< %= yield :special_script %>
< / head >
< body >
< p > Welcome! The date and time is < %= Time.now %>< / p >
< / body >
< / html >
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2014-05-21 21:47:18 -04:00
**app/views/articles/special.html.erb**
2009-09-03 23:35:18 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-03 23:35:18 -04:00
< p > This is a special page.< / p >
< % content_for :special_script do %>
2012-04-05 07:32:37 -04:00
< script > alert ( 'Hello!' ) < / script >
2010-08-14 01:13:00 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-03 23:35:18 -04:00
2012-09-01 17:25:58 -04:00
### DateHelper
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### date_select
2009-09-09 23:12:29 -04:00
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
date_select("article", "published_on")
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### datetime_select
2009-09-09 23:12:29 -04:00
Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
datetime_select("article", "published_on")
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### distance_of_time_in_words
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
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.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
distance_of_time_in_words(Time.now, Time.now + 15.seconds) # => less than a minute
2012-11-15 15:43:23 -05:00
distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true) # => less than 20 seconds
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_date
2009-09-09 23:12:29 -04:00
2014-08-08 17:25:18 -04:00
Returns a set of HTML select-tags (one for year, month, and day) pre-selected with the `date` provided.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Generates a date select that defaults to the date provided (six days after today)
select_date(Time.today + 6.days)
# Generates a date select that defaults to today (no specified date)
2010-08-14 01:13:00 -04:00
select_date()
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_datetime
2009-09-09 23:12:29 -04:00
2014-08-08 17:25:18 -04:00
Returns a set of HTML select-tags (one for year, month, day, hour, and minute) pre-selected with the `datetime` provided.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# 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)
2010-08-14 01:13:00 -04:00
select_datetime()
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_day
2009-09-09 23:12:29 -04:00
Returns a select tag with options for each of the days 1 through 31 with the current day selected.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Generates a select field for days that defaults to the day for the date provided
select_day(Time.today + 2.days)
# Generates a select field for days that defaults to the number given
select_day(5)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_hour
2009-09-09 23:12:29 -04:00
Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
2012-09-01 17:08:06 -04:00
```ruby
2013-07-11 11:20:42 -04:00
# Generates a select field for hours that defaults to the hours for the time provided
select_hour(Time.now + 6.hours)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_minute
2009-09-09 23:12:29 -04:00
Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Generates a select field for minutes that defaults to the minutes for the time provided.
2015-06-07 08:56:26 -04:00
select_minute(Time.now + 10.minutes)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_month
2009-09-09 23:12:29 -04:00
Returns a select tag with options for each of the months January through December with the current month selected.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Generates a select field for months that defaults to the current month
select_month(Date.today)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_second
2009-09-09 23:12:29 -04:00
Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Generates a select field for seconds that defaults to the seconds for the time provided
2015-06-08 11:12:06 -04:00
select_second(Time.now + 16.seconds)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_time
2009-09-09 23:12:29 -04:00
2014-08-08 17:25:18 -04:00
Returns a set of HTML select-tags (one for hour and minute).
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Generates a time select that defaults to the time provided
2010-08-14 01:13:00 -04:00
select_time(Time.now)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select_year
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the `:start_year` and `:end_year` keys in the `options` .
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-09-16 12:42:41 -04:00
# Generates a select field for five years on either side of Date.today that defaults to the current year
2009-09-09 23:12:29 -04:00
select_year(Date.today)
# Generates a select field from 1900 to 2009 that defaults to the current year
2012-11-15 15:43:23 -05:00
select_year(Date.today, start_year: 1900, end_year: 2009)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### time_ago_in_words
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
Like `distance_of_time_in_words` , but where `to_time` is fixed to `Time.now` .
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-11-15 15:43:23 -05:00
time_ago_in_words(3.minutes.from_now) # => 3 minutes
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### time_select
2009-09-09 23:12:29 -04:00
2018-05-08 22:48:07 -04:00
Returns a set of select tags (one for hour, minute, and optionally second) pre-selected for accessing a specified time-based attribute. The selects are prepared for multi-parameter assignment to an Active Record object.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
2010-08-14 01:13:00 -04:00
time_select("order", "submitted")
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
### DebugHelper
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
Returns a `pre` tag that has object dumped by YAML. This creates a very readable way to inspect an object.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-03-22 07:15:36 -04:00
my_hash = { 'first' => 1, 'second' => 'two', 'third' => [1,2,3] }
2009-09-09 23:12:29 -04:00
debug(my_hash)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```html
2009-09-09 23:12:29 -04:00
< pre class = 'debug_dump' > ---
first: 1
second: two
third:
- 1
- 2
- 3
< / pre >
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
### FormHelper
2009-09-09 23:12:29 -04:00
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). When the form is submitted (i.e., when the user hits the submit button or form.submit is called via JavaScript), the form inputs will be bundled into the params object and passed back to the controller.
2015-04-27 13:10:54 -04:00
There are two types of form helpers: those that specifically work with model attributes and those that don't. This helper deals with those that work with model attributes; to see an example of form helpers that don't work with model attributes, check the `ActionView::Helpers::FormTagHelper` documentation.
2009-09-09 23:12:29 -04:00
2015-04-27 13:10:54 -04:00
The core method of this helper, `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:
2009-09-09 23:12:29 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2009-09-09 23:12:29 -04:00
# Note: a @person variable will have been created in the controller (e.g. @person = Person.new)
2015-03-22 07:15:36 -04:00
< %= form_for @person , url: { action: "create" } do |f| %>
2009-09-09 23:12:29 -04:00
< %= f.text_field :first_name %>
< %= f.text_field :last_name %>
< %= submit_tag 'Create' %>
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
The HTML generated for this would be:
2012-09-01 17:08:06 -04:00
```html
2017-12-26 06:30:39 -05:00
< form class = "new_person" id = "new_person" action = "/people" accept-charset = "UTF-8" method = "post" >
< input name = "utf8" type = "hidden" value = "✓" / >
< input type = "hidden" name = "authenticity_token" value = "lTuvBzs7ANygT0NFinXj98tfw3Emfm65wwYLbUvoWsK2pngccIQSUorM2C035M9dZswXgWTvKwFS8W5TVblpYw==" / >
< input type = "text" name = "person[first_name]" id = "person_first_name" / >
< input type = "text" name = "person[last_name]" id = "person_last_name" / >
< input type = "submit" name = "commit" value = "Create" data-disable-with = "Create" / >
2009-09-09 23:12:29 -04:00
< / form >
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
The params object created when this form is submitted would look like:
2012-09-01 17:08:06 -04:00
```ruby
2017-12-26 06:30:39 -05:00
{"utf8" => "✓", "authenticity_token" => "lTuvBzs7ANygT0NFinXj98tfw3Emfm65wwYLbUvoWsK2pngccIQSUorM2C035M9dZswXgWTvKwFS8W5TVblpYw==", "person" => {"first_name" => "William", "last_name" => "Smith"}, "commit" => "Create", "controller" => "people", "action" => "create"}
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2017-12-26 06:30:39 -05:00
The params hash has a nested person value, which can therefore be accessed with `params[:person]` in the controller.
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### check_box
2009-09-09 23:12:29 -04:00
Returns a checkbox tag tailored for accessing a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# Let's say that @article.validated? is 1:
check_box("article", "validated")
# => <input type="checkbox" id="article_validated" name="article[validated]" value="1" />
# <input name="article[validated]" type="hidden" value="0" />
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### fields_for
2009-09-09 23:12:29 -04:00
2015-04-27 13:10:54 -04:00
Creates a scope around a specific model object like `form_for` , but doesn't create the form tags themselves. This makes `fields_for` suitable for specifying additional model objects in the same form:
2009-09-09 23:12:29 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2015-03-22 07:15:36 -04:00
< %= form_for @person , url: { action: "update" } do |person_form| %>
2009-09-09 23:12:29 -04:00
First name: < %= person_form.text_field :first_name %>
Last name : < %= person_form.text_field :last_name %>
2010-05-28 10:46:12 -04:00
< %= fields_for @person .permission do |permission_fields| %>
2009-09-09 23:12:29 -04:00
Admin? : < %= permission_fields.check_box :admin %>
< % end %>
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### file_field
2009-09-09 23:12:29 -04:00
2011-06-01 04:19:23 -04:00
Returns a file upload input tag tailored for accessing a specified attribute.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
file_field(:user, :avatar)
2010-08-14 01:13:00 -04:00
# => <input type="file" id="user_avatar" name="user[avatar]" />
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### form_for
2009-09-09 23:12:29 -04:00
Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
2012-09-03 21:21:24 -04:00
```html+erb
2014-05-21 21:47:18 -04:00
< %= form_for @article do |f| %>
2009-09-09 23:12:29 -04:00
< %= f.label :title, 'Title' %>:
2013-07-05 23:04:25 -04:00
< %= f.text_field :title %>< br >
2009-09-09 23:12:29 -04:00
< %= f.label :body, 'Body' %>:
2013-07-05 23:04:25 -04:00
< %= f.text_area :body %>< br >
2009-09-09 23:12:29 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### hidden_field
2009-09-09 23:12:29 -04:00
Returns a hidden input tag tailored for accessing a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
hidden_field(:user, :token)
2010-08-14 01:13:00 -04:00
# => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### label
2009-09-09 23:12:29 -04:00
Returns a label tag tailored for labelling an input field for a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
label(:article, :title)
# => <label for="article_title">Title</label>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### password_field
2009-09-09 23:12:29 -04:00
Returns an input tag of the "password" type tailored for accessing a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
password_field(:login, :pass)
2010-08-14 01:13:00 -04:00
# => <input type="text" id="login_pass" name="login[pass]" value="#{@login.pass}" />
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### radio_button
2009-09-09 23:12:29 -04:00
Returns a radio button tag for accessing a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# Let's say that @article.category returns "rails":
radio_button("article", "category", "rails")
radio_button("article", "category", "java")
# => <input type="radio" id="article_category_rails" name="article[category]" value="rails" checked="checked" />
# <input type="radio" id="article_category_java" name="article[category]" value="java" />
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### text_area
2009-09-09 23:12:29 -04:00
Returns a textarea opening and closing tag set tailored for accessing a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2012-11-15 15:43:23 -05:00
text_area(:comment, :text, size: "20x30")
2009-09-09 23:12:29 -04:00
# => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
# #{@comment.text}
# </textarea>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### text_field
2009-09-09 23:12:29 -04:00
Returns an input tag of the "text" type tailored for accessing a specified attribute.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
text_field(:article, :title)
# => <input type="text" id="article_title" name="article[title]" value="#{@article.title}" />
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2013-06-29 10:55:00 -04:00
#### email_field
Returns an input tag of the "email" type tailored for accessing a specified attribute.
```ruby
email_field(:user, :email)
# => <input type="email" id="user_email" name="user[email]" value="#{@user.email}" />
```
#### url_field
Returns an input tag of the "url" type tailored for accessing a specified attribute.
```ruby
url_field(:user, :url)
# => <input type="url" id="user_url" name="user[url]" value="#{@user.url}" />
```
2012-09-01 17:25:58 -04:00
### FormOptionsHelper
2009-09-09 23:12:29 -04:00
Provides a number of methods for turning different kinds of containers into a set of option tags.
2012-09-01 17:25:58 -04:00
#### collection_select
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
Returns `select` and `option` tags for the collection of existing return values of `method` for `object` 's class.
2009-09-09 23:12:29 -04:00
Example object structure for use with this method:
2012-09-01 17:08:06 -04:00
```ruby
2015-12-17 05:30:30 -05:00
class Article < ApplicationRecord
2009-09-09 23:12:29 -04:00
belongs_to :author
end
2015-12-17 05:30:30 -05:00
class Author < ApplicationRecord
2014-05-21 21:47:18 -04:00
has_many :articles
2009-09-09 23:12:29 -04:00
def name_with_initial
"#{first_name.first}. #{last_name}"
end
end
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2014-05-21 21:47:18 -04:00
Sample usage (selecting the associated Author for an instance of Article, `@article` ):
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-03-22 07:15:36 -04:00
collection_select(:article, :author_id, Author.all, :id, :name_with_initial, { prompt: true })
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2014-05-21 21:47:18 -04:00
If `@article.author_id` is 1, this would return:
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```html
2014-05-21 21:47:18 -04:00
< select name = "article[author_id]" >
2009-09-09 23:12:29 -04:00
< option value = "" > Please select< / option >
< option value = "1" selected = "selected" > D. Heinemeier Hansson< / option >
< option value = "2" > D. Thomas< / option >
< option value = "3" > M. Clark< / option >
< / select >
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### collection_radio_buttons
2012-02-01 16:16:58 -05:00
2012-09-01 21:37:59 -04:00
Returns `radio_button` tags for the collection of existing return values of `method` for `object` 's class.
2012-02-01 16:16:58 -05:00
Example object structure for use with this method:
2012-09-01 17:08:06 -04:00
```ruby
2015-12-17 05:30:30 -05:00
class Article < ApplicationRecord
2012-02-01 16:16:58 -05:00
belongs_to :author
end
2015-12-17 05:30:30 -05:00
class Author < ApplicationRecord
2014-05-21 21:47:18 -04:00
has_many :articles
2012-02-01 16:16:58 -05:00
def name_with_initial
"#{first_name.first}. #{last_name}"
end
end
2012-09-01 17:08:06 -04:00
```
2012-02-01 16:16:58 -05:00
2014-05-21 21:47:18 -04:00
Sample usage (selecting the associated Author for an instance of Article, `@article` ):
2012-02-01 16:16:58 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
collection_radio_buttons(:article, :author_id, Author.all, :id, :name_with_initial)
2012-09-01 17:08:06 -04:00
```
2012-02-01 16:16:58 -05:00
2014-05-21 21:47:18 -04:00
If `@article.author_id` is 1, this would return:
2012-02-01 16:16:58 -05:00
2012-09-01 17:08:06 -04:00
```html
2014-05-21 21:47:18 -04:00
< input id = "article_author_id_1" name = "article[author_id]" type = "radio" value = "1" checked = "checked" / >
< label for = "article_author_id_1" > D. Heinemeier Hansson< / label >
< input id = "article_author_id_2" name = "article[author_id]" type = "radio" value = "2" / >
< label for = "article_author_id_2" > D. Thomas< / label >
< input id = "article_author_id_3" name = "article[author_id]" type = "radio" value = "3" / >
< label for = "article_author_id_3" > M. Clark< / label >
2012-09-01 17:08:06 -04:00
```
2012-02-01 16:16:58 -05:00
2012-09-01 17:25:58 -04:00
#### collection_check_boxes
2012-02-01 16:16:58 -05:00
2012-09-01 21:37:59 -04:00
Returns `check_box` tags for the collection of existing return values of `method` for `object` 's class.
2012-02-01 16:16:58 -05:00
Example object structure for use with this method:
2012-09-01 17:08:06 -04:00
```ruby
2015-12-17 05:30:30 -05:00
class Article < ApplicationRecord
2013-06-25 18:35:03 -04:00
has_and_belongs_to_many :authors
2012-02-01 16:16:58 -05:00
end
2015-12-17 05:30:30 -05:00
class Author < ApplicationRecord
2014-05-21 21:47:18 -04:00
has_and_belongs_to_many :articles
2012-02-01 16:16:58 -05:00
def name_with_initial
"#{first_name.first}. #{last_name}"
end
end
2012-09-01 17:08:06 -04:00
```
2012-02-01 16:16:58 -05:00
2014-05-21 21:47:18 -04:00
Sample usage (selecting the associated Authors for an instance of Article, `@article` ):
2012-02-01 16:16:58 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
collection_check_boxes(:article, :author_ids, Author.all, :id, :name_with_initial)
2012-09-01 17:08:06 -04:00
```
2012-02-01 16:16:58 -05:00
2014-05-21 21:47:18 -04:00
If `@article.author_ids` is [1], this would return:
2012-02-01 16:16:58 -05:00
2012-09-01 17:08:06 -04:00
```html
2014-05-21 21:47:18 -04:00
< input id = "article_author_ids_1" name = "article[author_ids][]" type = "checkbox" value = "1" checked = "checked" / >
< label for = "article_author_ids_1" > D. Heinemeier Hansson< / label >
< input id = "article_author_ids_2" name = "article[author_ids][]" type = "checkbox" value = "2" / >
< label for = "article_author_ids_2" > D. Thomas< / label >
< input id = "article_author_ids_3" name = "article[author_ids][]" type = "checkbox" value = "3" / >
< label for = "article_author_ids_3" > M. Clark< / label >
< input name = "article[author_ids][]" type = "hidden" value = "" / >
2012-09-01 17:08:06 -04:00
```
2012-02-01 16:16:58 -05:00
2012-09-01 17:25:58 -04:00
#### option_groups_from_collection_for_select
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
Returns a string of `option` tags, like `options_from_collection_for_select` , but groups them by `optgroup` tags based on the object relationships of the arguments.
2009-09-09 23:12:29 -04:00
Example object structure for use with this method:
2012-09-01 17:08:06 -04:00
```ruby
2015-12-17 05:30:30 -05:00
class Continent < ApplicationRecord
2009-09-09 23:12:29 -04:00
has_many :countries
# attribs: id, name
end
2015-12-17 05:30:30 -05:00
class Country < ApplicationRecord
2009-09-09 23:12:29 -04:00
belongs_to :continent
# attribs: id, name, continent_id
end
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
Sample usage:
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
Possible output:
2012-09-01 17:08:06 -04:00
```html
2009-09-09 23:12:29 -04:00
< optgroup label = "Africa" >
< option value = "1" > Egypt< / option >
< option value = "4" > Rwanda< / option >
...
< / optgroup >
< optgroup label = "Asia" >
< option value = "3" selected = "selected" > China< / option >
< option value = "12" > India< / option >
< option value = "5" > Japan< / option >
...
< / optgroup >
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2018-03-12 07:39:24 -04:00
NOTE: Only the `optgroup` and `option` tags are returned, so you still have to wrap the output in an appropriate `select` tag.
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### options_for_select
2009-09-09 23:12:29 -04:00
2010-08-14 01:13:00 -04:00
Accepts a container (hash, array, enumerable, your type) and returns a string of option tags.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
options_for_select([ "VISA", "MasterCard" ])
# => <option>VISA</option> <option>MasterCard</option>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2018-03-12 07:39:24 -04:00
NOTE: Only the `option` tags are returned, you have to wrap this call in a regular HTML `select` tag.
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### options_from_collection_for_select
2009-09-09 23:12:29 -04:00
2012-09-01 21:37:59 -04:00
Returns a string of option tags that have been compiled by iterating over the `collection` and assigning the result of a call to the `value_method` as the option value and the `text_method` as the option text.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
# options_from_collection_for_select(collection, value_method, text_method, selected = nil)
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2015-04-27 13:10:54 -04:00
For example, imagine a loop iterating over each person in `@project.people` to generate an input tag:
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-09 23:12:29 -04:00
options_from_collection_for_select(@project.people, "id", "name")
# => <option value="#{person.id}">#{person.name}</option>
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2018-03-12 07:39:24 -04:00
NOTE: Only the `option` tags are returned, you have to wrap this call in a regular HTML `select` tag.
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### select
2009-09-09 23:12:29 -04:00
Create a select tag and a series of contained option tags for the provided object and method.
2011-05-29 09:05:42 -04:00
Example:
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-03-22 07:15:36 -04:00
select("article", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { include_blank: true })
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2014-05-21 21:47:18 -04:00
If `@article.person_id` is 1, this would become:
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```html
2014-05-21 21:47:18 -04:00
< select name = "article[person_id]" >
2009-09-09 23:12:29 -04:00
< option value = "" > < / option >
< option value = "1" selected = "selected" > David< / option >
2015-07-02 19:21:28 -04:00
< option value = "2" > Eileen< / option >
< option value = "3" > Rafael< / option >
2009-09-09 23:12:29 -04:00
< / select >
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### time_zone_options_for_select
2009-09-09 23:12:29 -04:00
Returns a string of option tags for pretty much any time zone in the world.
2012-09-01 17:25:58 -04:00
#### time_zone_select
2009-09-09 23:12:29 -04:00
2013-12-02 09:05:02 -05:00
Returns select and option tags for the given object and method, using `time_zone_options_for_select` to generate the list of option tags.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2017-10-11 20:10:58 -04:00
time_zone_select("user", "time_zone")
2012-09-01 17:08:06 -04:00
```
2009-09-09 23:12:29 -04:00
2013-06-15 02:56:04 -04:00
#### date_field
Returns an input tag of the "date" type tailored for accessing a specified attribute.
```ruby
date_field("user", "dob")
```
2012-09-01 17:25:58 -04:00
### FormTagHelper
2009-09-09 23:12:29 -04:00
2013-12-02 09:05:02 -05:00
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.
2009-09-09 23:12:29 -04:00
2012-09-01 17:25:58 -04:00
#### check_box_tag
2009-09-09 23:12:29 -04:00
2009-09-11 18:58:37 -04:00
Creates a check box form input tag.
2009-09-09 23:12:29 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
check_box_tag 'accept'
2010-08-14 01:13:00 -04:00
# => <input id="accept" name="accept" type="checkbox" value="1" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### field_set_tag
2009-09-11 18:58:37 -04:00
Creates a field set for grouping HTML form elements.
2012-09-03 21:21:24 -04:00
```html+erb
2010-04-06 19:52:39 -04:00
< %= field_set_tag do %>
2009-09-11 18:58:37 -04:00
< p > < %= text_field_tag 'name' %>< / p >
< % end %>
# => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### file_field_tag
2009-09-11 18:58:37 -04:00
2010-08-14 01:13:00 -04:00
Creates a file upload field.
2009-09-11 18:58:37 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2015-03-22 07:15:36 -04:00
< %= form_tag({ action: "post" }, multipart: true) do %>
2009-09-11 18:58:37 -04:00
< label for = "file" > File to Upload< / label > < %= file_field_tag "file" %>
< %= submit_tag %>
2010-04-06 19:52:39 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
Example output:
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
file_field_tag 'attachment'
# => <input id="attachment" name="attachment" type="file" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### form_tag
2009-09-11 18:58:37 -04:00
2016-02-24 08:05:24 -05:00
Starts a form tag that points the action to a URL configured with `url_for_options` just like `ActionController::Base#url_for` .
2009-09-11 18:58:37 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2014-05-21 21:47:18 -04:00
< %= form_tag '/articles' do %>
2009-09-11 18:58:37 -04:00
< div > < %= submit_tag 'Save' %>< / div >
2010-04-06 19:52:39 -04:00
< % end %>
2014-05-21 21:47:18 -04:00
# => <form action="/articles" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### hidden_field_tag
2009-09-11 18:58:37 -04:00
Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or data that should be hidden from the user.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
# => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### image_submit_tag
2009-09-11 18:58:37 -04:00
Displays an image which when clicked will submit the form.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
image_submit_tag("login.png")
# => <input src="/images/login.png" type="image" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### label_tag
2009-09-11 18:58:37 -04:00
Creates a label field.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
label_tag 'name'
# => <label for="name">Name</label>
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### password_field_tag
2009-09-11 18:58:37 -04:00
Creates a password field, a masked text field that will hide the users input behind a mask character.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
password_field_tag 'pass'
# => <input id="pass" name="pass" type="password" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### radio_button_tag
2009-09-11 18:58:37 -04:00
Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.
2012-09-01 17:08:06 -04:00
```ruby
2018-04-15 10:42:56 -04:00
radio_button_tag 'favorite_color', 'maroon'
# => <input id="favorite_color_maroon" name="favorite_color" type="radio" value="maroon" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### select_tag
2009-09-11 18:58:37 -04:00
Creates a dropdown selection box.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
select_tag "people", "< option > David< / option > "
# => <select id="people" name="people"><option>David</option></select>
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### submit_tag
2009-09-11 18:58:37 -04:00
Creates a submit button with the text provided as the caption.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
submit_tag "Publish this article"
# => <input name="commit" type="submit" value="Publish this article" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### text_area_tag
2009-09-11 18:58:37 -04:00
Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
text_area_tag 'article'
# => <textarea id="article" name="article"></textarea>
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### text_field_tag
2009-09-11 18:58:37 -04:00
Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
text_field_tag 'name'
# => <input id="name" name="name" type="text" />
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2013-06-29 10:55:00 -04:00
#### email_field_tag
Creates a standard input field of email type.
```ruby
email_field_tag 'email'
# => <input id="email" name="email" type="email" />
```
#### url_field_tag
Creates a standard input field of url type.
```ruby
url_field_tag 'url'
# => <input id="url" name="url" type="url" />
```
2013-06-15 02:56:04 -04:00
#### date_field_tag
Creates a standard input field of date type.
```ruby
date_field_tag "dob"
# => <input id="dob" name="dob" type="date" />
```
2012-09-01 17:25:58 -04:00
### JavaScriptHelper
2009-09-11 18:58:37 -04:00
Provides functionality for working with JavaScript in your views.
2012-09-01 17:25:58 -04:00
#### escape_javascript
2009-09-11 18:58:37 -04:00
Escape carrier returns and single and double quotes for JavaScript segments.
2012-09-01 17:25:58 -04:00
#### javascript_tag
2009-09-11 18:58:37 -04:00
Returns a JavaScript tag wrapping the provided code.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
javascript_tag "alert('All is good')"
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:08:06 -04:00
```html
2012-04-05 07:32:37 -04:00
< script >
2009-09-11 18:58:37 -04:00
//< ![CDATA[
alert('All is good')
//]]>
< / script >
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
### NumberHelper
2009-09-11 18:58:37 -04:00
Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.
2012-09-01 17:25:58 -04:00
#### number_to_currency
2009-09-11 18:58:37 -04:00
Formats a number into a currency string (e.g., $13.65).
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
number_to_currency(1234567890.50) # => $1,234,567,890.50
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### number_to_human_size
2009-09-11 18:58:37 -04:00
Formats the bytes in size into a more understandable representation; useful for reporting file sizes to users.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
number_to_human_size(1234) # => 1.2 KB
number_to_human_size(1234567) # => 1.2 MB
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### number_to_percentage
2009-09-11 18:58:37 -04:00
Formats a number as a percentage string.
2012-09-01 17:08:06 -04:00
```ruby
2013-07-08 04:18:53 -04:00
number_to_percentage(100, precision: 0) # => 100%
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### number_to_phone
2009-09-11 18:58:37 -04:00
2016-04-05 02:12:07 -04:00
Formats a number into a phone number (US by default).
2009-09-11 18:58:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
number_to_phone(1235551234) # => 123-555-1234
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### number_with_delimiter
2009-09-11 18:58:37 -04:00
Formats a number with grouped thousands using a delimiter.
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
number_with_delimiter(12345678) # => 12,345,678
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2012-09-01 17:25:58 -04:00
#### number_with_precision
2009-09-11 18:58:37 -04:00
2012-09-01 21:37:59 -04:00
Formats a number with the specified level of `precision` , which defaults to 3.
2009-09-11 18:58:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2009-09-11 18:58:37 -04:00
number_with_precision(111.2345) # => 111.235
2016-07-06 01:15:09 -04:00
number_with_precision(111.2345, precision: 2) # => 111.23
2012-09-01 17:08:06 -04:00
```
2009-09-11 18:58:37 -04:00
2013-07-26 12:15:05 -04:00
### SanitizeHelper
The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
#### sanitize
2014-08-08 17:25:18 -04:00
This sanitize helper will HTML encode all tags and strip all attributes that aren't specifically allowed.
2013-07-26 12:15:05 -04:00
```ruby
sanitize @article .body
```
2015-06-07 09:07:12 -04:00
If either the `:attributes` or `:tags` options are passed, only the mentioned attributes and tags are allowed and nothing else.
2013-07-26 12:15:05 -04:00
```ruby
sanitize @article .body, tags: %w(table tr td), attributes: %w(id class style)
```
To change defaults for multiple uses, for example adding table tags to the default:
```ruby
class Application < Rails::Application
config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
end
```
#### sanitize_css(style)
Sanitizes a block of CSS code.
2014-03-08 07:41:38 -05:00
#### strip_links(html)
2013-07-26 12:15:05 -04:00
Strips all link tags from text leaving just the link text.
```ruby
2015-08-23 08:07:05 -04:00
strip_links('< a href = "http://rubyonrails.org" > Ruby on Rails< / a > ')
2013-07-26 12:15:05 -04:00
# => Ruby on Rails
```
```ruby
2015-08-23 08:07:05 -04:00
strip_links('emails to < a href = "mailto:me@email.com" > me@email.com< / a > .')
2013-07-26 12:15:05 -04:00
# => emails to me@email.com.
```
```ruby
strip_links('Blog: < a href = "http://myblog.com/" > Visit< / a > .')
# => Blog: Visit.
```
2014-03-08 07:41:38 -05:00
#### strip_tags(html)
2013-07-26 12:15:05 -04:00
2014-03-08 07:41:38 -05:00
Strips all HTML tags from the html, including comments.
2016-11-19 20:31:25 -05:00
This functionality is powered by the rails-html-sanitizer gem.
2013-07-26 12:15:05 -04:00
```ruby
strip_tags("Strip < i > these< / i > tags!")
# => Strip these tags!
```
```ruby
strip_tags("< b > Bold< / b > no more! < a href = 'more.html' > See more< / a > ")
# => Bold no more! See more
```
2013-08-23 11:59:11 -04:00
NB: The output may still contain unescaped '< ', '>', '& ' characters and confuse browsers.
2013-07-26 12:15:05 -04:00
2014-03-08 07:41:38 -05:00
### CsrfHelper
Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
request forgery protection parameter and token, respectively.
```html
2014-03-08 09:46:43 -05:00
< %= csrf_meta_tags %>
2014-03-08 07:41:38 -05:00
```
2014-03-08 09:46:43 -05:00
NOTE: Regular forms generate hidden fields so they do not use these tags. More
details can be found in the [Rails Security Guide ](security.html#cross-site-request-forgery-csrf ).
2013-07-26 12:15:05 -04:00
2012-09-01 17:25:58 -04:00
Localized Views
---------------
2009-04-05 19:29:03 -04:00
2015-02-17 23:01:15 -05:00
Action View has the ability to render different templates depending on the current locale.
2009-04-05 19:29:03 -04:00
2016-02-13 06:36:16 -05:00
For example, suppose you have an `ArticlesController` with a show action. By default, calling this action will render `app/views/articles/show.html.erb` . But if you set `I18n.locale = :de` , then `app/views/articles/show.de.html.erb` will be rendered instead. If the localized template isn't present, the undecorated version will be used. This means you're not required to provide localized views for all cases, but they will be preferred and used if available.
2009-04-05 19:29:03 -04:00
2012-09-01 21:37:59 -04:00
You can use the same technique to localize the rescue files in your public directory. For example, setting `I18n.locale = :de` and creating `public/500.de.html` and `public/404.de.html` would allow you to have localized rescue pages.
2009-04-05 19:29:03 -04:00
2012-09-01 21:37:59 -04:00
Since Rails doesn't restrict the symbols that you use to set I18n.locale, you can leverage this system to display different content depending on anything you like. For example, suppose you have some "expert" users that should see different pages from "normal" users. You could add the following to `app/controllers/application.rb` :
2009-04-05 19:29:03 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-12-07 23:43:46 -05:00
before_action :set_expert_locale
2009-04-05 19:29:03 -04:00
def set_expert_locale
I18n.locale = :expert if current_user.expert?
end
2012-09-01 17:08:06 -04:00
```
2009-04-05 19:29:03 -04:00
2014-05-21 21:47:18 -04:00
Then you could create special views like `app/views/articles/show.expert.html.erb` that would only be displayed to expert users.
2009-04-05 19:29:03 -04:00
2012-09-02 01:08:20 -04:00
You can read more about the Rails Internationalization (I18n) API [here ](i18n.html ).