Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:28:42+00:00 2026-06-03T06:28:42+00:00

I would like to populate options for collection_select based on previous collection_select selection. I

  • 0

I would like to populate options for collection_select based on previous collection_select selection. I have in one selection Sports and I would like to populate next selection with teams based on that sport.

Problem is that all examples that I found are for older versions of ROR, so I decided to ask here. I know i have to add some nasty things like :

html_options = {:onchange=>'d=$("#player_sports_id").val();$.ajax({type: "GET", url: "/players/returnsel1.js?sports_id="+d, dataType: "script" });'})

to call player#returnsel1 and get returned returnsel1.js.erb.
In returnsel1 I have variable

@teamstoreturn=Teams.where(:sport_id=params[:sport_id])

So how should jquery returnsel1.js.erb looks like?
Thank you.
Dorijan

edit1:
here is how it actually looks, but it does not work:

html_options = {:onchange=>'d=$("#player_sports_id").val();$.ajax({type: "GET", url: "/players/returnsel1?sports_id="+d, dataType: "script",success: function(data){ $("#player_teams_id").html(data)} });'})

and in controller it is like this:

  def returnsel1
    @teamstoreturn=Team.where(:sports_id=>params[:sports_id])
    render :inline => "<%= collection_select(:player, :teams_id, @teamstoreturn, :id, :naziv) %>"
  end

if I go to /players/returnsel1?sports_id=2 I got correct drop down menu

what have I done wrong?

edit2:
there should be dataType: “html” instead dataType: “script”
and now it is working 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:28:44+00:00Added an answer on June 3, 2026 at 6:28 am

    You should extend the .ajax request with a function, which can handle the successful answer. Without this, the answer is discarder. Propably this function should contain a jquery code, to change the other selections content.

    html_options = {:onchange=>'d=$("#player_sports_id").val();$.ajax({type: "GET", url: "/players/returnsel1.js?sports_id="+d, dataType: "script", success: function(data){ $("#other_box_part").html(data)} });'})
    

    This way you change this element, so you must render this as a html fragment, which must be inserted. So no javascript code in the answer is required, only html. E.g.:

    <%= select_tag ... %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that I would like to populate data when the next
I have two divs. I would like to move/populate the text from div id
I have a DDL and ASP.NET Textbox. I would like to populate the text
I have 2 tables, Provinces and Districts. I would like to populate a select
I would like to populate a DateTime structure from a string, but it is
I would like to populate a combobox with the following: Visible item / Item
I would like to pre-populate a UITextfield with the phone number of the current
I'm trying to populate a ComboBox programatically. I am creating ComboBoxItems and would like
I have a 'created_date' DATETIME field which I would like to be populated with
Would like to make anapplication in Java that will not automatically parse parameters used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.