[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

binding input elements to params and sessions too

henkvantijen

12/16/2006 1:36:00 AM

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?

1 Answer

David Vallner

12/16/2006 7:49:00 PM

0

henkvantijen@gmail.com 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 Vallner