# Best practices - field initialization based on display rules

**URL:** <https://www.ruby-forum.com/t/best-practices-field-initialization-based-on-display-rules/98724>\
**Category:** Rails\
**Created:** [July 17, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/best-practices-field-initialization-based-on-display-rules/98724 "2006-07-17T19:48:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 17, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/best-practices-field-initialization-based-on-display-rules/98724/1 "2006-07-17T19:48:46Z")

</div>

All,

I have a form with the following field interaction rules.

Given checkbox A,  
text field B  
text field C

If checkbox A is checked then make text field C equal to the value of  
text field B and disable field C.

If checkbox A is unchecked then enable field C.

In order to display a form where I’m manipulating an already existing  
object, I have something like this in my RHTML template to display text  
field C:

\<%= (@obj.field\_B == @obj.field\_C) ?  
text\_field(:obj, ‘field\_C’, :disabled =\> ‘true’) :  
text\_field(:obj, ‘field\_C’, :disabled =\> ‘false’) %\>

Should I put this logic in a helper so as to simplify my template and  
keep this small bit of business logic out of it?

I already have Javascript that would force this behavior for me given  
the checked status of the checkbox - should I just call that Javascript  
function in an onLoad() handler?

Should I use an onLoad handler that executes an AJAX call to handle  
setting up these fields correctly - then I could just add AJAX calls to  
default other fields as well? (seems a little excessive to me).

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 17, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/best-practices-field-initialization-based-on-display-rules/98724/2 "2006-07-17T20:39:44Z")

</div>

On Monday, July 17, 2006, at 7:48 PM, Wes G. wrote:

> Should I put this logic in a helper so as to simplify my template and  
> Thanks,  
> Wes
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I would do this in two steps…

First… use your controller to set the proper state of instance  
variables so that the rules are enforced when an object is loaded in.  
This way you don’t need any JS to play around with the initial settings.  
You can also set the enabled/disabled state of controls like this.

Second… I would use the KRJS plugin to enforce the rules once the page  
has been loaded. It’s pretty simple to add an AJAX response to an  
‘onchange’ event and then embed the RJS in the controller. This keeps  
the business logic out of the view.

In theory you could write a JS library that does all the validation on  
the client side, but I wouldn’t trust that route. It’s not very DRY.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 17, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/best-practices-field-initialization-based-on-display-rules/98724/3 "2006-07-17T21:16:29Z")

</div>

Kevin O. wrote:

> On Monday, July 17, 2006, at 7:48 PM, Wes G. wrote:
> 
> > Should I put this logic in a helper so as to simplify my template and  
> > Thanks,  
> > Wes
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> I would do this in two steps…
> 
> First… use your controller to set the proper state of instance  
> variables so that the rules are enforced when an object is loaded in.  
> This way you don’t need any JS to play around with the initial settings.  
> You can also set the enabled/disabled state of controls like this.
> 
> Second… I would use the KRJS plugin to enforce the rules once the page  
> has been loaded. It’s pretty simple to add an AJAX response to an  
> ‘onchange’ event and then embed the RJS in the controller. This keeps  
> the business logic out of the view.
> 
> In theory you could write a JS library that does all the validation on  
> the client side, but I wouldn’t trust that route. It’s not very DRY.
> 
> \_Kevin  
> [www.sciwerks.com](http://www.sciwerks.com)

Kevin,

I take your point on the DRYness of a client-side approach. On the  
other hand, what I’m needing to do in the view doesn’t really need any  
information from the server so doing a POST just to do AJAX somehow  
seems excessive (probably because it’s ingrained in me that doing the  
POST is expensive, even though I know it’s not because it’s not the  
whole page worth of data going back and forth). But I see your point  
and I’d consider an AJAX approach.

However, I’m not sure I get you on this point:

“You can also set the enabled/disabled state of controls like this.”

Do you mean set up a variable in my model to represent the state of the  
edit field in my view (e.g. enabled vs. disabled)? If so, I would think  
that that sort of thing would belong in a “form model” object (what in  
the Java world we call a form bean, which is simply responsible for  
tracking the state of form elements).

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 17, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/best-practices-field-initialization-based-on-display-rules/98724/4 "2006-07-17T22:49:13Z")

</div>

On Monday, July 17, 2006, at 9:16 PM, Wes G. wrote:

> > > Rails mailing list  
> > > Second… I would use the KRJS plugin to enforce the rules once the page
> 
> Thanks,  
> Wes
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Something like that. I sometimes put code in my views like this…

\<%= text\_field ‘model’,‘attribute’, :disabled=\>@model.some\_method %\>

this works when the page is first loaded to set the initial state but  
you then need to worry about it from then on.

Regarding the AJAX thing… I just do what is easiest to implement and  
then optimize it later if it becomes a problem. Using something like  
the KRJS plugin, it’s almost trivial to implement this sort of thing.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
