← Back to Upcase

Best practice for coding show/edit templates?

(Jon Seidel) #1

I’m curious to see how folks code their show & edit templates. Everything I’ve seen says you code them separately, but I never liked having to duplicate the code/layouts. So… I worked out a way to use the same code for both show/edit. I’m wondering how else folks handle this and/or what you think of my approach.

  1. I code the form/fields in a partial… say _form_body.html.haml
  2. I render this partial in both the show/edit templates
  3. I have a javascript function that fires on page load and – if it’s a show template – disables all the form fields so that they display but aren’t editable.

Certainly worked to DRY up my templates. However, it does mean that I need to code my rspec examples differently, using “expect(page).to have_css(‘input[id=“ee_requisition_requisition_number”]’, value: ‘EB-0001’)” instead of the “have_content” matcher.

Seeral years ago, I had coded up some helpers to handle this so that edit requests generated form fields and show requests generated simple text elements, but found the approach above was simpler and easier to maintain.