Topic: Params and AJAX, dealing with dynamic select pulldowns

I have a select pulldown, with an observe_field linked to it.  When the select changes, I call a method that needs to populate a second select pulldown depending upon which item was selected in the first.  I can get it to create the second pulldown when you select an item in the first, but will the first pulldown send it's selected item via the params when it changes?  Sounds confusing, I'm sure, but here's some code for what I'm trying to do:

# in list.rhtml, first pulldown
<% form_remote_tag :url => { :action => 'populate_second_pulldown' } do %>
        <%= collection_select("club", "selected_club", @clubs, "value", "value") %>
        <%= submit_tag("test") %>
   
      <%= observe_field "club[selected_club]", :update => "",
      :with => "", :url => { :action => 'populate_second_pulldown' } %>
<% end %>

# 2nd pulldown in list.rhtml
<div id="leaders-select">
      <% form_remote_tag :url => { :action => 'test2' } do %>
          <select class="pulldown">
          <option>--Select A Leader--</option>
        </select name="leader[leader_selected]">
        <%= submit_tag("test2") %>
        
        <%= observe_field "leader[leader_selected]", :update => "",
        :with => "", :url => { :action => 'test2' } %>

        <% end %>
</div>

# populate_second_pulldown.rjs
page.replace_html("leaders-select", :partial => 'leaders')

# _leaders.rhtml partial
<% form_remote_tag :url => { :action => 'test2' } do %>
  <%= collection_select("leader", "selected_leader",@leaders, "last_name", "last_name") %>
  <%= submit_tag("test2") %>
        
  <%= observe_field "leader[leader_selected]", :update => "",
  :with => "", :url => { :action => 'test2' } %>
<% end %>

# populate_second_pulldown action in controller
session[:selected_club] = params[:club][:selected_club]


At the moment I have it set up to call the associated method by either selecting an item (onchange) or by clicking the submit button.  That's mostly for testing at the moment.  But what I'm trying to do is select a club from the "club" pulldown, and then populate the "leader" pulldown with all the leaders in the selected club.  However, in the controller, when I set the session[:selected_club] to the params, it doesn't receive a value.  Is there something special I need to do in order to receive the params hash for an AJAX request, or how would I access the params? 

I know this is a lengthy question, so if anyone just knows of a good site or tutorial that would help, I'd greatly appreciate it.  I've spent a good deal of time searching this morning, with no luck.  Thanks for any help or pointers.

Re: Params and AJAX, dealing with dynamic select pulldowns

You're setting the :with option to an empty string in the observe_field call. Out of curiosity, why? Try removing the :with option entirely. I think by default it includes the value of the field here.

Take a look at the development log when selecting an item to see what they call it. It may be in params[:value], I'm not sure exactly.

Railscasts - Free Ruby on Rails Screencasts

Re: Params and AJAX, dealing with dynamic select pulldowns

I left the :with blank because I wasn't sure what to do with them yet.  I'm still trying to learn how to implement AJAX.  Thanks for the help; I'll check my log and see what it says.

Re: Params and AJAX, dealing with dynamic select pulldowns

I checked the log, and when you select an item from the pulldown, no params are being sent (besides the controller and action).  But when I select the submit button for the form instead of using the observe_field onchange to call the method, it sends the parameters as I'd expect.  Is there any way to have the select form "click" the submit button when I just select an item in the pulldown?

Re: Params and AJAX, dealing with dynamic select pulldowns

Did you remove the blank ":with" option?

Railscasts - Free Ruby on Rails Screencasts

Re: Params and AJAX, dealing with dynamic select pulldowns

I removed the blank :with tags.  But it still does the same thing.  It only sends the selected item parameter when I click the submit button; it doesn't send the param when I select an item.

Re: Params and AJAX, dealing with dynamic select pulldowns

I believe I have it.  If anyone else is wondering, when you select the item in the pulldown, and use the observe_field to make a request, if you essentially want to submit some params via the params hash, you should put their values in the :with portion of your observe_field.  Hope this may help any other newbies out there like me.

Re: Params and AJAX, dealing with dynamic select pulldowns

That's strange, it should default to passing the value. You can try manually supplying "with" like this:

<%= observe_field "club[selected_club]", with => "'club[selected_club]=' + value", :url => { :action => 'populate_second_pulldown' } %>

Or try this:

<%= observe_field "club_selected_club", :url => { :action => 'populate_second_pulldown' } %>

Check the development log with both of those and see if it changes anything.

Railscasts - Free Ruby on Rails Screencasts

Re: Params and AJAX, dealing with dynamic select pulldowns

ryanb, I checked the log with the 2nd part, and that didn't work.  It wouldn't send the selected value via the params hash.  I can't see the first code you posted; for some reason I see the code tags, and the "fold" and "unfold", but there's no code itself.  But I assume you had something like this?:

<%= observe_field "club_selected_club", :url => { :action => 'populate_second_pulldown' },
:with => "club_selected_club" %>

This seems to work.  I checked the log, and this sends "club[selected_club]" via the params hash, which is what I needed.  Is this an appropriate methodology, or do you know of a better/more effective way of passing the value?  And thanks for all your help today smile

Re: Params and AJAX, dealing with dynamic select pulldowns

I don't fully understand how the :with option behaves with observe_field, but if that last code you posted works then that's great. smile

I'm not exactly sure why it works, but can't complain.

Railscasts - Free Ruby on Rails Screencasts

Re: Params and AJAX, dealing with dynamic select pulldowns

Hi guys,

From what I've just read, the default :params that the observe_field will pass is "value"

From the rails api:

:with:    A JavaScript expression specifying the parameters for the XMLHttpRequest. [i]This defaults to

Posts [ 11 ]

Pages 1