Welcome Guest | Login

popluate select tag from choice of another select tag

Hello,

What I want to do is have a user select an option from a dropdown, and have another dropdown populate after the first selection.  My code is below:

<p><label for="league">League</label> | <label for="team">Team</label><br/>
<%= select_tag('blogs', '<option>League1</option>,<option>League2</option>,<option>League3</option>,<option>League4</option>') %> |
<%= collection_select(:teams, :id, @teams, :id, :city) %></p>

I'm basically looking for a Rails equivalent of PostBack in ASP.  Any help would be great.

Thanks!

2007-08-07 09:04 PM

You need to call an Ajax.Updater onSelect.  Look at the code that Rails outputs for something like link_to_remote and you can copy that in and change the syntax accordingly.  If you need further help with this let me know.  

2007-08-08 02:06 AM

I'm not sure how to code an Updater - all I found was to create a javascript script in my head that contains "new Ajax.Updater...".  Also, I'm not sure how I would code in the onSelect.

2007-08-08 04:40 PM

ahh - sorry - I mean onChange. forgive me.  See this page for more information.

You would essentially put the Ajax.Updater in the function that gets called onChange, and it would update a div that would be populated by your new select box with custom options.  Does that make sense?

2007-08-08 05:23 PM

ok, so far I'm trying this:
<script type="text/javascript">
function change_league()
{
new Ajax.Updater(‘leagues’, ‘/update/’, {asynchronous:true})
}
</script>
<p><label for="league">League</label>
<select name=leagues onChange="change_league()">
<option value=''>Select League<option value="NHL">NHL</option>
<option value="NFL">NFL</option><option value="NBA">NBA</option>
<option value="NHL">NHL</option><option value="MLB">MLB</option><br />
</select>
<label for="team">Team</label><br/>
<div id="leagues">
</div>
1) Am I on the right track?  2) How do I specify the correct action to occur instead of '/update/'?  3) How do I pass the value of the selected league into the model?

Thanks!

2007-08-08 05:48 PM

I've used this tutorial for something similar.

2007-08-09 01:02 AM

phenomenal tutorial - thanks!  But I have to have those selects outside of the form for submission of a new blog.  I'm using a partial for the 'create' form - it contains things like
<%= text_field 'blogs', 'team_id', :value => session[:team] %>
That shows the table (blogs) and the value (team_id) to be passed into the database.  How do i make it so that the select I just created passes the correct value (team_id insteand of team_name) into the database?  Right now, I can't get it to pass any value!

2007-08-09 08:50 PM

I would suggest not using text_field, just use the html
<input type="text" name="blogs[team_id]" value="<%= session[:team] -%>">
This way you can put anything else in there you need.

So, the new select can just have the team_id in there manually.

Let us know if you need more help.

2007-08-11 01:55 PM

Sorry for the confusion - the text field was just an example of what I'm using to pass other values.  Let me start from the beginning.  I am using the scaffold "create" function to create an entry into a table (blogs) in my database.  One of the fields in that table is "team_id", which corresponds to my "teams" table.  In order to get the select lists to populate correctly, I had to put them in a separate form.  I now have two questions: 1) How can I make the value of the select list different from the display name (value = team_id, text = team_name)?  2) How do I pass the value of the select tag into the "create" form, so the value gets passed into the database?

2007-08-11 08:54 PM

Lets back up first

-----------
In order to get the select lists to populate correctly, I had to put them in a separate form.
-----------
Why?  

---------
How can I make the value of the select list different from the display name (value = team_id, text = team_name)?
---------

<option value="your_desired_value">your_desired_display_name</option>


----------
2) How do I pass the value of the select tag into the "create" form, so the value gets passed into the database?
----------
<select name="myselect">
 <option value="something">somethingelsemaybe</option>
 ....
</select>

then in your controller action you can handle it with @something = params[:something]

then make your database call, for example:

@record = Record.new
@record.foo = @something
@record.save


I hope this helps?


2007-08-12 07:06 AM

The lists had to be in a separate form because I was using a button to populate the second list.  Now, I'm just using HTML, and I have this as my first dropdown:
<select name='league', onchange="new Ajax.Updater('teams_div', '/team/update_teams', {asynchronous:true, evalScripts:true, insertion:Insertion.Top, onSuccess:function(request){$('teams_div').innerHTML=''}, parameters:Form.serialize(this)}); return false;"> 
<option>Select League</option><option>NHL</option>
<option>NFL</option><option>NBA</option><option>MLB</option>
</select>
<div id='teams_div'></div>
However, when populating the second list, it seems like the parameter isn't being passed through - I get a "You have a nil object..." error.  How do I pass the value of the selected league into the controller? I thought the "parameters:..." would do it, but apparently not.

2007-08-12 08:52 AM

Your

parameters:Form.serialize(this)

is trying to serialize your select object.

You need to replace this with

parameters:Form.serialize($('your_form_id'))

Let us know if that helps.  

2007-08-13 03:23 AM

I got it by having the leagues / teams in a separate form from what i'm submitting to the DB.  I think included hidden forms whose values were populated when a team was selected.

thanks for your help!

2007-08-26 01:44 PM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!