Observe_field submit behavior

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

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

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

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

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
:wink:

Best regards,
Bill

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs