Académique Documents
Professionnel Documents
Culture Documents
K e e p i n g Yo u r V i e w s D RY
G l e n n Va n d e r b u r g glv@vanderburg.org
Think About It
A new Rails project gives you:
A place for everything, and everything in its place; Help creating test data; Help writing tests; rake stats to give you feedback; Automation for all the mundane stuff; A Rakele with all the targets that you shouldve had on every project you ever worked on (but didnt); A starting-point application with no broken windows.
Sure, there are always some issues. But for the most part, things are good. Except for the views.
Isnt that normal? (Its not just Rails every web framework wrestles with this problem.)
Also
Helpers arent very well understood. They can do several different kinds of things. Making them work like the built-in helpers takes practice. Writing them involves parts of Rails that arent used much in the rest of your application.
How Not To Do It
This is Better
Larger amounts?
Build strings using <<EOS, %{}, StringIO Use Builder::XmlMarkup render :partial =>
Reusability
Don't overdesign to make something reusable. On the other hand, taking HTML options is easy and goes a long way.
Generating JavaScript
How Not To Do It
This Is Better
JavaScript Helpers
Rails Recipes, Recipe 2.
Buy it, read it, use it.
When existing helpers can generate JavaScript for you, use them.
Even from within other helpers.
Use application.js
Included automatically with javascript_include_tag :defaults
Learn JavaScript!
JavaScript is a fun language, if you know it well. Use it as it was meant to be used.
Use objects. Put functions in objects to keep the global namespace clean.
Learn Prototype!
Prototype has lots of cool stuff to make your JavaScript better. And it's documented now. :-)
Form Builders
Form Builders
Designed to encapsulate form styles that are used across your application. Form elds usually come with labels and a bunch of style info. They might be in lists or table cells or special divs. It's a mess.
A Complex Helper
Demo
HTML Required
id_eld id_submit_button id_busy busyid
add_trigger_for_id
id_eld_autocomplete
JavaScript Required
Show the form and hide the link when the link is clicked. Submit an autocomplete request when keys are pressed. Show/hide autocomplete status image. Submit add request. Show/hide Adding status message. Add new item to the list when request returns. Highlight it to show the change.
Tips (Review)
Strive
Be intolerant of messiness and duplication in your views. When you put logic in views, build helpers.
Anything more than simple conditionals
Follow Conventions
The built-in helpers have strong conventions for parameters and options. Its easy to follow those conventions in your own helpers. Your teammates (current and future) will thank you.
Stand on Shoulders
How are the built-in helpers implemented? Do you know? Why not? Click the show source link.