# Binding input elements to params and sessions too

**URL:** <https://www.ruby-forum.com/t/binding-input-elements-to-params-and-sessions-too/79533>\
**Category:** Ruby\
**Created:** [December 16, 2006, 2:40am UTC](https://www.ruby-forum.com/t/binding-input-elements-to-params-and-sessions-too/79533 "2006-12-16T02:40:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 2:40am UTC](https://www.ruby-forum.com/t/binding-input-elements-to-params-and-sessions-too/79533/1 "2006-12-16T02:40:58Z")

</div>

Hi,

I started experimenting with RoR and came across the raw support for  
form elements, if the input element is not bound to a Active Record  
object. In my case, I was puzzled that I could find no better than this  
for a set of filter checkboxes on top of a listing:

```
<%= check_box_tag :indexes, 1, checked = @indexes %> Indexes
<%= check_box_tag :stocks, 1, checked = @stocks %> Stocks
<%= check_box_tag :rates, 1, checked = @rates %> Rates

```

In the controller I also need some plumbing to connect the params to  
the proper vars:

```
@stocks = params[:stocks]
@indexes = params[:indexes]
@rates = params[:rates]

```

Not very DRY.

In contrast, for an input element (without \_tag) that I can bind to an  
AR object, I only have to say

```
<%= text_field 'prediction', 'predicted_value' %>

```

and I don’t need the plumbing: the binding is 2-way automagically.

Q: Is there some way to say something like  
\<%= check\_box\_tag :params :indexes %\> Indexes  
if I want to bind the input element to the previous form value, and  
\<%= check\_box\_tag :session :indexes %\> Indexes  
if I want to bind the input element 2-way to the session var ?

So perhaps what I am saying is, lets use a :binding\_type key, that  
defaults to active record:

```
  <%= text_field 'prediction', 'predicted_value' %> # is default

```

case of next line

```
<%= text_field :binding_type => :active_record, 'prediction',

```

‘predicted\_value’ %\>

```
<%= text_field :binding_type => :params, 'predicted_value' %> #

```

2-way

```
<%= text_field :binding_type => :sessions, 'predicted_value' %> #

```

2-way

```
    <%= check_box :binding_type => :sessions, :indexes %> # 2-way

```

and so on.

Hey, I am new to RoR, so I may have these wild ideas, okay?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/binding-input-elements-to-params-and-sessions-too/79533/2 "2006-12-16T20:50:07Z")

</div>

removed\_email\_address@domain.invalid wrote:

I’ll leave the usual redirect to the RoR lists to someone with a canned  
speech for that, so here goes.

Firstly, I think the views have access to the request object.

Also, the controller “preparing data” for the view is a common pattern  
for web frameworks.

> So perhaps what I am saying is, lets use a :binding\_type key, that  
> defaults to active record:

I don’t think that information belongs to the view. A view template  
should be oblivious to the controller and model technology, or even to  
the fact it’s used in a web framework with sessions and request  
parameters. That way, you could technically use (even if the probability  
of that happening is minuscule) the same template in a non-Rails  
application as long as the instvars of its evaluation context get  
appropriately set.

Besides, it’s even more verbose than just setting the instvars in the  
controller.

If you do that too often, do something like this in your configuration /  
environment / whatever (been a while since I touched Rails) files:

class ActionController::Base  
def autoregister\_params(\*args)  
keys = args.empty? ?  
params.keys :  
args

```
# Call me old-fashioned, but I like this better than each ;P
for key in keys
  instance_variable_set('@#{key}', params[key])
end

```

end  
end

And then in your controller just use autoregister\_params(), or for your  
example, autoregister\_params(:stocks, :indexes, :rates) (to avoid  
cluttering the instvar namespace with things you only need in the  
controller.)

Or, if very lazy / DRY-addled, I’m sure there’s some AoP -ish meta  
voodoo or Rails hook to cause autoregister\_params to be called for each  
request.

David V.
