diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-18 17:34:37 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-18 17:34:37 -0200 |
commit | e07eb079bf6911e520edfb8a40677aa26a4987de (patch) | |
tree | 35be3d8e7aada158c9ebde7abb55b83f4cc80f54 /guides/source | |
parent | ef4afb65b2d4797ee23d9e4e7a64a13746052d24 (diff) | |
parent | 9999dc947fa45fcc2ca7d27dfcf04ebd078acda1 (diff) | |
download | rails-e07eb079bf6911e520edfb8a40677aa26a4987de.tar.gz rails-e07eb079bf6911e520edfb8a40677aa26a4987de.tar.bz2 rails-e07eb079bf6911e520edfb8a40677aa26a4987de.zip |
Merge pull request #15700 from maurogeorge/guides-local_assigns
Add docs about local_assigns on guides
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/layouts_and_rendering.md | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/guides/source/layouts_and_rendering.md b/guides/source/layouts_and_rendering.md index b7fc580939..329d501ce0 100644 --- a/guides/source/layouts_and_rendering.md +++ b/guides/source/layouts_and_rendering.md @@ -1124,6 +1124,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 |