joeb,
Ross R.'s suggestion is good if you hare having issues with your
controller’s, but I believe your are talking about your VIEW. I had
this same problem as well. I have a MASSIVE form, some several hundred
lines and I found myself updating my edit form but forgetting to make
coresponding changes in my show. So I came up with a hack.
First, I create a partial and try to follow this convention:
_form.rthml
Field Label |
<%= text_field :object, :method %> |
<%= @object.method %> |
######
This is a very simple approach which lets you put your input and output
right next to each other - very easy to manage because everything is in
one place.
Then in my views that use the partial, I use javascript to hide the
part of the form which I don’t want to use. Example:
new.rhtml or edit.rhtml
<% @page_heading = “Page Name” %>
<% @body_onload = “hideOutput();” %>
<%= start_form_tag …%>
<%= render :partial => “form” %>
<%= end form_tag %>
The @page_heading and @body_onload are references in my layout that
allow me to uniformly display the page’s title and a heading as well as
add javascript to body’s onload tag.
The javascript to do this is fairly tricky and another hack which I dug
up on the web. You can have javascript read the class tag’s to see if
it maches what you’re looking for. I found these functions and tweaked
them for my own needs. Unfortunately, I can’t remember exactly how
they work. I know it has something to do with the order of your CSS
rules. (Maybe someone else could suggest some improvements for this
section.)
appliaction.js
########
function getRules() {
var theRules = new Array();
if (document.styleSheets[0].cssRules) {
theRules = document.styleSheets[0].cssRules;
} else if (document.styleSheets[0].rules) {
theRules = document.styleSheets[0].rules;
}
return theRules;
}
function hideInput(){
var css = getRules();
css[0].style.display = ‘none’; //hide input class, (i.e. the
css[0] would be first line of css rule)
css[2].style.fontSize = ‘medium’; //make label size smaller
css[3].style.fontSize = ‘medium’; //make required label smaller size
css[4].style.backgroundColor = ‘white’; //make modifiable label
background white
css[4].style.fontSize = ‘medium’; //make modifiable label smaller
size
css[3].style.backgroundColor = ‘white’; //make modifiable label
background white
}
function hideOutput(){
var css = getRules();
css[1].style.display = ‘none’; //hide output class, (the second line
of css)
}
###########
So to wrap this up, this approach gives a couple of great pros:
(1) One place for both input and output in your forms, so you can’t get
input and output “out-of-sync” with each other.
(2) You’re using CSS so you get a lot of controller over what your form
looks like.
(3) You can use Javascript to hack up the views even further if need
be. (For example, when printing, you can just remove all the output
fields so it prints nicely.)
This approach is not without it’s cons as well:
(1) It’s kind of a hack and who knows if it will work for everyone.
(This is my first time sharing it and I’d love some feedback.)
(2) I’ve not tested its cross-browser compatibility. I’m lucky I only
have to worry about IE.
(3) Using the line numbers of your CSS is very brittle and could break
by simply adding an new line of CSS to the top.
(4) Your form can get VERY large. My form is now a 946 lines, but is
also the core of my application. I’m sure it will get refactored time
and again into smaller bits, but it certainly can inflate the size of
your partials.
I hope this helps and I’d appricate any suggestions.
Brian