2

I have a bunch of data in a single model that I want to display based on collection select (drop down) box. The section data looks like this...

class CreateSections < ActiveRecord::Migration
  def change
    create_table :sections do |t|
      t.string :supervisor
      t.string :catagory
      t.string :title
      t.string :description
      t.string :days
      t.string :start_date
      t.string :end_date
      t.string :start_time
      t.string :end_time
      t.string :course_id
      t.string :room
      t.string :building
      t.string :location
      t.string :tuition
      t.string :lab_fee
      t.string :insurance_fee
      t.string :technology_fee

      t.timestamps
    end
  end
end

My collection select looks like this...

  collection_select(:section, :section_id, @sections, :id, :title)

So the user will select the title from the drop down, and I would like to display the description. Ideally I would like to do this with jquery.

EDIT

Here is more info on what the html looks like...

 <select id="section_section_id" name="section[section_id]"><option value="1">Long Title</option>
<option value="2"> Alternative Investments for Small Business  NEW</option>
<option value="3">Bookkeeping Basics for the Natural Products Industry  NEW  </option>
...

In app/assets/javascripts/sections.js I now have...

$(function(){
  $("#section_section_id").change(function(){
    alert('wintas');
  })
})

So now based on the option that is selected, I would like to display the value associated with that option.

2
  • what related data you want to show when an option is selected ? Commented Apr 2, 2013 at 19:13
  • So you can see in the collection select I am displaying title. There is a description field in the same model/table. When the user selects a title I would like to display the associated description. Commented Apr 3, 2013 at 15:15

1 Answer 1

2

You can use options_for_select to place the info you want to display in data attribute of that options

Assuming you have the select in context of a form

<%= f.select :section, :section_id, options_for_select(@sections.map{ |s| [s.title, s.id, {'data-description'=>s.description}] }) %>

This will generate the html like this

<option value="2" data-description= "description of 2"> Alternative Investments for Small Business  NEW</option>
<option value="3" data-description="description of 3">Bookkeeping Basics for the Natural Products Industry  NEW  </option>

You can then use js to show it

Here is a working demo of js

http://jsfiddle.net/Tt3TS/

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.