AJAX select: How to send the selected value to AJAX action?

All,

I want to have a select box which sends it’s selected value to my action
onChange.

Here’s what I have so far (there is no form):

<%= select(:preferences, :job_history_filter_period,
ContactPreference::JOB_HISTORY_OPTIONS, {},
:onChange => remote_function(:url => {:action =>
:modify_history_time_period})) %>

What is the correct way to get the selected value of the list box into
the request?

I assume that I have to modify the :parameters option of the Ajax
request, but not sure how to do that with the helper.

Thanks,
Wes

You can do this:

<%= select(:preferences, :job_history_filter_period,
ContactPreference::JOB_HISTORY_OPTIONS, {},
html_options = {
:onChange => “new Ajax.Request(
‘/controller_name/modify_history_time_period/’ +
this[this.selectedIndex].value,
{asynchronous:true, evalScripts:true});”
}
) %>

It’s not as clean as the helper but gets the job done.

Cheers,
Atha

How quickly we forget:

The solution:

<%= select(:preferences, :job_history_filter_period,
ContactPreference::JOB_HISTORY_OPTIONS, {},
:onChange => remote_function(:url => {:action =>
:modify_history_time_period},
:with => “‘time_period=’ +
this.options[this.selectedIndex].value”)) %>

I need to help with the documentation effort - there is no mention of
the :with parameter anywhere in the API docs. for remote_function, nor
link_to_remote. If you look in the source for the options_for_ajax()
function, you’ll find the use of the :with parameter. But that seems a
little more difficult than it should be.

Thanks,
Wes

You could also use a two-pronged approach:

<%= … select box code :preferences, :job_history_filter_period %>

<%= observe_field 'preferences[job_history_filter_period], :update =>
‘some
field’, :url => {:action => :modify_history_time_period} %>

Though I do notice that you shouldn’t need the :with in the first place.
The
Rails helpers should automatically send back
params[:job_history_filter_period] through remote_function, though I may
be
wrong.

Jason

You can do this:

<%= select(:preferences,
:job_history_filter_period,
ContactPreference::JOB_HISTORY_OPTIONS,
options = {},
html_options = {
:onChange => “new
Ajax.Request(‘/controller_name/modify_history_time_period/’ +
this[this.selectedIndex].value, {asynchronous:true,
evalScripts:true});”
}
) %>

It’s not as clean as the helper but gets the job done.

Cheers,
Atha

On Jan 17, 3:06 pm, Wes G. [email protected]

Jason R. wrote:

You could also use a two-pronged approach:

<%= … select box code :preferences, :job_history_filter_period %>

<%= observe_field 'preferences[job_history_filter_period], :update =>
‘some
field’, :url => {:action => :modify_history_time_period} %>

Though I do notice that you shouldn’t need the :with in the first place.
The
Rails helpers should automatically send back
params[:job_history_filter_period] through remote_function, though I may
be
wrong.

Jason

Jason,

You’re right that I probably could have used an observer, but I don’t
need to update anything on the page. I just need to notify the server
that something has changed.

The “automatically sending back params[:job_history_filter_period]”
would happen if I were sending form contents back, but I’m not.
Originally, I tried that (with a form_remote_tag around the select), but
it wouldn’t work because you can’t use JS to submit a form. So I ended
up with this approach, which is the most direct, I think.

Thanks,
Wes