Ruby on Rails
UnderstandingPartials

%{color:green}WikiGardening required. There are conflicting explainations of 1.3’s implementation.
%

Partials are reasonably documented in the API documentation for partials and better documented in an improved version .

Note: partial behaviour changed between 0.11, 0.12 and 0.13.

The “middle parameter” (@nil@) in the 0.11 version was unintuitive in its behaviour and has been deprecated. Notice also the usage of strings vs. symbols for variable names.

Be careful to make sure that you assign the local variable created by the partial into a new instance variable if you want to use text_field or other form helper methods which assume the existence of an instance variable based on the “name” parameter passed to them.

For example, given a partial named x.rhtml, calling textfield and passing it :x will not work because there is no @x instance variable. You need to assign @x = x inside of your partial to be able to use form_helper methods.

Optional Locals

You can also make locals function similarly to optional method parameters (e.g. def fxn(req_param, opt_param = nil)) by using “if local_assigns[:param].nil?”, (but not in HAML, just RHTML) as in


  title = nil if local_assigns[:title].nil?
  other = nil if local_assigns[:other].nil?

You can read more about this in the How-To Make a Rails Partial With Optional Locals

See Also

category: Understanding

This must be re-written

A recursive turorial is never good.