# Observe\_field submit behavior

**URL:** <https://www.ruby-forum.com/t/observe-field-submit-behavior/99342>\
**Category:** Rails\
**Created:** [July 21, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/observe-field-submit-behavior/99342 "2006-07-21T20:25:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [July 21, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/observe-field-submit-behavior/99342/1 "2006-07-21T20:25:34Z")

</div>

I am using observe\_field on a number of multi-selects and checkbox form  
elements whose values would normally come in as arrays on a normal  
(non-Ajax) form post.

I understand that by using the :with =\> ‘name’ I will get name=value in  
the request.raw\_post however if the field I am submitting has mutliple  
values and the name is of the form ‘name[]’, I get  
name[]=[‘value1,value2,value3’]. I need to get  
name[]=[‘name1’,‘name2’,‘name3’] just as if I had submitted the entire  
form.

Either I

1. am not doing something right,
2. must write some kind of javascript or rjs to force the data into the  
correct form so it is parsed properly by the Rails POST processing, or
3. must write code to split on ‘,’ in my controller or helper.

Am I missing something or is there a better way?

Steven

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [July 21, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/observe-field-submit-behavior/99342/2 "2006-07-21T21:27:47Z")

</div>

Hi Steven,

Steven wrote:

> I understand that by using the :with =\> ‘name’ I will get  
> name=value in the request.raw\_post

Not sure if you already know it and just choose to do it differently,  
but  
:with=\>‘name’ also returns the value in params[:name]. I haven’t used  
the  
multi-select yet but I’d make a small wager that using :with=\>‘name[]’  
might  
allow access via something like params[:name][0], params[:name][1], etc.  
since params is a hash. If I had time (but don’t right now) I’d dummy  
up a  
quick view for the controller with \<%= params.inspect %\> (commenting out  
whatever redirect\_to it’s doing right now or renaming the current view)  
and  
see if the values from the multi-select are, in fact, getting returned./  
can  
be accessed via params. Hope you’ll let us know.

Best regarsds,  
Bill

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [July 21, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/observe-field-submit-behavior/99342/3 "2006-07-21T22:06:53Z")

</div>

Bill W. wrote:

> Hi Steven,
> 
> Steven wrote:
> 
> > I understand that by using the :with =\> ‘name’ I will get  
> > name=value in the request.raw\_post
> 
> Not sure if you already know it and just choose to do it differently,  
> but :with=\>‘name’ also returns the value in params[:name]. I haven’t used  
> the multi-select yet but I’d make a small wager that using :with=\>‘name[]’  
> might allow access via something like params[:name][0], params[:name][1], etc.  
> since params is a hash.

That is precisely the problem. It seems to me not doing this violates  
the principle of least surprise.

All the options are crammed into params[:name][0] in a comma separated  
list.

I have this in my view:

\<%= select\_tag ‘counties[]’, options\_for\_select(@county\_names,  
@search\_criteria.counties), {:multiple =\> true, :size =\> 8, :class =\>  
“infoSelectForm” } %\>  
\<%= observe\_field ‘counties[]’, :url =\> {:action =\> :update\_counties},  
:with =\> ‘counties[]’ %\>

When I set a breakpoint I can see the following:

irb(#SearchCriteria:0x25573a4):001:0\> params  
=\> {“counties”=\>[“County1,County2”], “action”=\>“update\_counties”,  
“controller”=\>“search\_criteria”}

This just does not seem like the most natural behavior for the :with  
option.

Should I poke into the code and see if it can be fixed? I am not  
familiar with the process for submitting fixes. I am not even sure  
where to start and I’d rather not run a patched Rails.

Steven

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [July 21, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/observe-field-submit-behavior/99342/4 "2006-07-21T23:22:24Z")

</div>

Steven wrote:

> Bill W. wrote:
> 
> > Hi Steven,
> > 
> > Steven wrote:
> > 
> > > I understand that by using the :with =\> ‘name’ I will get  
> > > name=value in the request.raw\_post
> > 
> > Not sure if you already know it and just choose to do it differently,  
> > but :with=\>‘name’ also returns the value in params[:name]. I haven’t used  
> > the multi-select yet but I’d make a small wager that using :with=\>‘name[]’  
> > might allow access via something like params[:name][0], params[:name][1], etc.  
> > since params is a hash.
> 
> That is precisely the problem. It seems to me not doing this violates  
> the principle of least surprise.
> 
> All the options are crammed into params[:name][0] in a comma separated  
> list.
> 
> I have this in my view:
> 
> \<%= select\_tag ‘counties[]’, options\_for\_select(@county\_names,  
> @search\_criteria.counties), {:multiple =\> true, :size =\> 8, :class =\>  
> “infoSelectForm” } %\>  
> \<%= observe\_field ‘counties[]’, :url =\> {:action =\> :update\_counties},  
> :with =\> ‘counties[]’ %\>
> 
> When I set a breakpoint I can see the following:
> 
> irb(#SearchCriteria:0x25573a4):001:0\> params  
> =\> {“counties”=\>[“County1,County2”], “action”=\>“update\_counties”,  
> “controller”=\>“search\_criteria”}
> 
> This just does not seem like the most natural behavior for the :with  
> option.

It seems even more broken for a set of check\_box\_tags where the name is  
of the form ‘name[]’. In a POST, these all come through as an array.  
observe\_field needs to observe each individual checkbox. This requires  
a lot of smelly code. Some changes could be made to observe\_field to  
make it work if you named each check\_box\_tag ‘name[]’ but gave it a  
unique :id =\> “name-#{model.id}”

Another way would be to create an observe\_multiple\_fields method that  
takes a pattern.

Another way, is to add code to submit the whole form for each  
observe\_field. This is the approach I am currently exploring… as it  
would seem to require least code added.

The whole idea though would be to get the params hash to work as  
naturally as possible for observe\_field.

Steven

---

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [July 22, 2006, 2:42am UTC](https://www.ruby-forum.com/t/observe-field-submit-behavior/99342/5 "2006-07-22T02:42:43Z")

</div>

Hi Steven,

Steven wrote:

> That is precisely the problem. It seems to me not doing this violates  
> the principle of least surprise.
> 
> All the options are crammed into params[:name][0] in a comma separated  
> list.
> 
> I have this in my view:
> 
> \<%= select\_tag ‘counties[]’,

Again, I haven’t used the multiple select option but, what that line is  
asking for as a return is, as I understand it, an array not a hash.

> \<%= observe\_field ‘counties[]’,

Now that I look at this it seems kind of counter intuitive. How would  
one  
watch an array? IIRC, the default for select fields is onchange. I’d  
try,  
just for grins and giggles, to see what it returns if you just  
observe\_field  
‘counties’. My guess is that it’ll either throw a syntax error or  
something  
more interesting.

> irb(#SearchCriteria:0x25573a4):001:0\> params  
> =\> {“counties”=\>[“County1,County2”],

Yep. Just like we asked it, I think. “I’m looking for an array.” “OK.  
Here it is.”

> This just does not seem like the most natural behavior  
> for the :with option.

I wouldn’t argue that.

> Should I poke into the code and see if it can be fixed?

Without reading a bunch of xhtml spec stuff, I’m not sure if it needs to  
be  
fixed. I’ve hit stuff like this a couple of times before that, to me,  
was  
totally counter-intuitive yet, there it was, right there in the spec.  
Easiest thing to do is just split the array in your controller. You’ll  
be  
past this in 2 minutes. Crusades can be fun, but they’re time consuming  
😉

Best regards,  
Bill
