You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/action_view_overview.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,14 +16,14 @@ What is Action View?
16
16
17
17
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.
18
18
19
-
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.
19
+
Action View templates are written using embedded Ruby in tags mingled with HTML. To avoid cluttering the templates with boilerplate code, several helper classes provide common behavior for forms, dates, and strings. It's also easy to add new helpers to your application as it evolves.
20
20
21
21
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.
22
22
23
23
Using Action View with Rails
24
24
----------------------------
25
25
26
-
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.
26
+
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.
27
27
28
28
Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
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.
46
46
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.
47
-
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.
47
+
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.
48
48
49
49
50
50
Templates, Partials, and Layouts
@@ -150,7 +150,7 @@ end
150
150
151
151
[Jbuilder](https://github.com/rails/jbuilder) is a gem that's
152
152
maintained by the Rails team and included in the default Rails `Gemfile`.
153
-
It's similar to Builder, but is used to generate JSON, instead of XML.
153
+
It's similar to Builder but is used to generate JSON, instead of XML.
154
154
155
155
If you don't have it, you can add the following to your `Gemfile`:
156
156
@@ -182,7 +182,7 @@ more examples and information.
182
182
183
183
#### Template Caching
184
184
185
-
By default, Rails will compile each template to a method in order to render it. In the development environment, when you alter a template, Rails will check the file's modification time and recompile it.
185
+
By default, Rails will compile each template to a method to render it. In the development environment, when you alter a template, Rails will check the file's modification time and recompile it.
186
186
187
187
### Partials
188
188
@@ -268,7 +268,7 @@ we would do:
268
268
<%= render partial: "product", object: @item %>
269
269
```
270
270
271
-
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:
271
+
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:
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.
285
+
Commonly, 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.
286
286
287
287
So this example for rendering all the products:
288
288
@@ -298,7 +298,7 @@ can be rewritten in a single line:
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.
301
+
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.
302
302
303
303
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:
304
304
@@ -371,14 +371,14 @@ View Paths
371
371
----------
372
372
373
373
When rendering a response, the controller needs to resolve where the different
374
-
views are located. By default it only looks inside the `app/views` directory.
374
+
views are located. By default, it only looks inside the `app/views` directory.
375
375
376
-
We can add other locations and give them a certain precedence when resolving
376
+
We can add other locations and give them certain precedence when resolving
377
377
paths using the `prepend_view_path` and `append_view_path` methods.
378
378
379
379
### Prepend view path
380
380
381
-
This can be helpful for example, when we want to put views inside a different
381
+
This can be helpful for example when we want to put views inside a different
0 commit comments