Views are a Mess

Rails apps are pretty well designed, except for the Views.

 

Reasons - mix of languages, HTML, Ruby, JavaScript (every web framework has this problem).

Refactoring views involves writing helpers.

 

Helpers are not Well Understood

Making them work takes practise.

 

HTML Helpers

Mutliline string concatenation, or multiline string.

But, built-in helpers use content_tag( :tagname, :class => 'classname') - better than HTML, because now we're not mixing languages.

Another built-in helper: concat, capture.

Render partial from helper!

 

Reusability comes from reusing something

Rather than designing for reusability...

But for helpers, use html_options hash, and pass on to  HTML helpers.

If generating multiple tags, html options should apply to outermost tag, and css selectors can be used to define inner css.

 

Helpers that generate JavaScript

Avoid putting javascript event handlers into HTML tags. Use observe_field prototype helper instead. Detangles javascript from HTML tag.

 

Learn JavaScript

Use objects, user Protoype (well documented now).

 

Form Builders

Introduced with Rails 1.2.

 

Demo

Lots of JavaScript !!!