diff options
Diffstat (limited to 'railties/doc/guides/actionview')
-rw-r--r-- | railties/doc/guides/actionview/helpers.markdown | 20 | ||||
-rw-r--r-- | railties/doc/guides/actionview/partials.markdown | 15 |
2 files changed, 17 insertions, 18 deletions
diff --git a/railties/doc/guides/actionview/helpers.markdown b/railties/doc/guides/actionview/helpers.markdown index c191c5e5ef..c702e83ff9 100644 --- a/railties/doc/guides/actionview/helpers.markdown +++ b/railties/doc/guides/actionview/helpers.markdown @@ -16,11 +16,11 @@ Helpers allow you to encapsulate rendering tasks as reusable functions. Helpers In this way, methods in the menu helper are made available to any view or partial in your application. These methods can accept parameters, for example controller instance variables (eg; records or record collections gathered by you current controller), items from the view or partial’s locals[] hash or items from the params[] hash. You may wish to pass your controller instance variables and items from the params[] hash to the locals hash before rendering (See the section on partials). Helper methods can also accept an executable block of code. -It is important to remember, though, that helpers are for rendering, and that they become available once a controller method has returned, while Rails is engaged in rendering the contents generated by a controller method. This means that helper methods are not available from within the methods of your controllers. +It is important to remember, though, that helpers are for rendering, and that they become available once a controller method has returned, while Rails is engaged in rendering the contents generated by a controller method. This means that helper methods are not available from within the methods of your controllers. -Helpers can accomplish a variety of tasks, from formatting a complex tag for embedding content for a browser plugin (eg; Flash), to assembling a menu of options appropriate for the current context of your application, to generating sections of forms that get assembled on-the-fly. +Helpers can accomplish a variety of tasks, from formatting a complex tag for embedding content for a browser plugin (eg; Flash), to assembling a menu of options appropriate for the current context of your application, to generating sections of forms that get assembled on-the-fly. -Helpers are organized around rendering tasks, so it is not necessary (nor necessarily desirable) to organize them around your application’s controllers or models. In fact, one of the benefits of helpers is that they are not connected via a rendering pipeline to specific controllers, like views and partials are. They can and should handle more generalized tasks. +Helpers are organized around rendering tasks, so it is not necessary (nor necessarily desirable) to organize them around your application’s controllers or models. In fact, one of the benefits of helpers is that they are not connected via a rendering pipeline to specific controllers, like views and partials are. They can and should handle more generalized tasks. Here is a very simple, pseudo-example: @@ -60,18 +60,18 @@ In general, the choice between using a partial vs. using a helper depends on the Tutorial -- Calling a Helper [UNFINISHED] ------------------------ -1. Create a Rails application using `rails helper_test` -Notice the code: - +1. Create a Rails application using `rails helper_test` +Notice the code: + class ApplicationController < ActionController::Base helper :all # include all helpers, all the time For this tutorial, we'll keep this code, but you will likely want to exert more control over loading your helpers. - + 2. Configure a database of your choice for the app. -3. Inside of the `/app/helpers/` directory, create a new file called, `menu_helper.rb`. Write this in the file: +3. Inside of the `/app/helpers/` directory, create a new file called, `menu_helper.rb`. Write this in the file: - module MenuHelpers + module MenuHelpers def menu(records, item_proc=nil) items = records.collect{ |record| menu_item(record, item_proc) @@ -86,6 +86,6 @@ For this tutorial, we'll keep this code, but you will likely want to exert more end end -4. Create a scaffold for some object in your app, using `./script/generate scaffold widgets`. +4. Create a scaffold for some object in your app, using `./script/generate scaffold widgets`. 5. Create a database table for your widgets, with at least the fields `name` and `id`. Create a few widgets. 6. Call the menu command twice from `index.html.erb`, once using the default action, and once supplying a Proc to generate urls.
\ No newline at end of file diff --git a/railties/doc/guides/actionview/partials.markdown b/railties/doc/guides/actionview/partials.markdown index 9f387fbafb..2988b933bc 100644 --- a/railties/doc/guides/actionview/partials.markdown +++ b/railties/doc/guides/actionview/partials.markdown @@ -1,9 +1,9 @@ A Guide to Using Partials =============================== -This guide elaborates on the use and function of partials in Ruby on Rails. As your Rails application grows, your view templates can start to contain a lot of duplicate view code. To manage and reduce this complexity, you can by abstract view template code into partials. Partials are reusable snippets of eRB template code stored in separate files with an underscore ('_') prefix. +This guide elaborates on the use and function of partials in Ruby on Rails. As your Rails application grows, your view templates can start to contain a lot of duplicate view code. To manage and reduce this complexity, you can by abstract view template code into partials. Partials are reusable snippets of eRB template code stored in separate files with an underscore ('_') prefix. -Partials can be located anywhere in the `app/views` directory. File extensions for partials work just like other template files, they bear an extension that denotes what kind of code they generate. For example, `_animal.html.erb` and `_animal.xml.erb` are valid filenames for partials. +Partials can be located anywhere in the `app/views` directory. File extensions for partials work just like other template files, they bear an extension that denotes what kind of code they generate. For example, `_animal.html.erb` and `_animal.xml.erb` are valid filenames for partials. Partials can be inserted in eRB template code by calling the `render` method with the `:partial` option. For example: @@ -20,7 +20,7 @@ Abstracting views into partials can be approached in a number of different ways, Partials as a View Subroutine ----------------------------- -Using the `:locals` option, you can pass a hash of values which will be treated as local variables within the partial template. +Using the `:locals` option, you can pass a hash of values which will be treated as local variables within the partial template. <%= render :partial => "person", :locals => { :name => "david" } %> @@ -34,7 +34,7 @@ So that you can later check: <p>Hello, <%= name %>!</p> <% end -%> -Otherwise, the if statement will throw an error at runtime. +Otherwise, the if statement will throw an error at runtime. Another thing to be aware of is that instance variables that are visible to the parent view template are visible to the partial. So you might be tempted to do this: @@ -46,7 +46,7 @@ And then within the partial: <p>Hello, <%= @name %>!</p> <% end -%> -The potential snag here is that if multiple templates start to rely on this partial, you will need to maintain an instance variable with the same name across all of these templates and controllers. This approach can quickly become brittle if overused. +The potential snag here is that if multiple templates start to rely on this partial, you will need to maintain an instance variable with the same name across all of these templates and controllers. This approach can quickly become brittle if overused. Partials as a View of an Object -------------------------------- @@ -61,7 +61,7 @@ If the instance variable `name` in the parent template matches the name of the p render :partial => "person" -Now the value that was in `@person` in the parent template is accessible as `person` in the partial. +Now the value that was in `@person` in the parent template is accessible as `person` in the partial. Partials as a View of a Collection ----------------------------------- @@ -83,9 +83,8 @@ Where `@winners` contains three people, produces the following output: 2) Jeff 3) Nick -One last detail, you can place an arbitrary snippet of code in between the objects using the `:spacer_template` option. +One last detail, you can place an arbitrary snippet of code in between the objects using the `:spacer_template` option. # Renders the same collection of partials, but also renders the # person_divider partial between each person partial. render :partial => "person", :collection => @winners, :spacer_template => "person_divider" - |