aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/layouts_and_rendering.md
diff options
context:
space:
mode:
authorMauro George <maurogot@gmail.com>2014-06-13 18:39:05 -0300
committerMauro George <maurogot@gmail.com>2014-06-13 19:01:29 -0300
commit24bcfed15dceb16641896d4b1a875e043d863221 (patch)
treea5f5d3fe7daabd598b07bfa27cf434bfad3d6f53 /guides/source/layouts_and_rendering.md
parent4c0a1021e51bfb8f3623480f577f7f52d83a0fc7 (diff)
downloadrails-24bcfed15dceb16641896d4b1a875e043d863221.tar.gz
rails-24bcfed15dceb16641896d4b1a875e043d863221.tar.bz2
rails-24bcfed15dceb16641896d4b1a875e043d863221.zip
Add docs about local_assigns on guides
[ci skip]
Diffstat (limited to 'guides/source/layouts_and_rendering.md')
-rw-r--r--guides/source/layouts_and_rendering.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/guides/source/layouts_and_rendering.md b/guides/source/layouts_and_rendering.md
index 5b75540c05..e8d91b0369 100644
--- a/guides/source/layouts_and_rendering.md
+++ b/guides/source/layouts_and_rendering.md
@@ -1069,6 +1069,36 @@ You can also pass local variables into partials, making them even more powerful
Although the same partial will be rendered into both views, Action View's submit helper will return "Create Zone" for the new action and "Update Zone" for the edit action.
+To pass a local variable to a partial in only specific cases use the `local_assigns`.
+
+* `index.html.erb`
+
+ ```erb
+ <%= render user.articles %>
+ ```
+
+* `show.html.erb`
+
+ ```erb
+ <%= render article, full: true %>
+ ```
+
+* `_articles.html.erb`
+
+ ```erb
+ <%= content_tag_for :article, article do |article| %>
+ <h2><%= article.title %></h2>
+
+ <% if local_assigns[:full] %>
+ <%= simple_format article.body %>
+ <% else %>
+ <%= truncate article.body %>
+ <% end %>
+ <% end %>
+ ```
+
+This way it is possible to use the partial without the need to declare all local variables.
+
Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the `:object` option:
```erb