how to obtain the index of selected item from select box?

Hai friends,

    I am a newbie in ROR.I hav created a select box which takes value from the databaseI dont know how to get the id or index of the value selected.

Controller:

class ReadingsController < ApplicationController def index @locations= Station.find(:all,:group => "state",:order =>"state") end end

View:

<% form_for :station ,:url => {:action => 'index',:id => @station } do

form> %>

<center><b> <%= form.label :Stations %></b> <%= form.collection_select("state", @locations, :id, :state) %> </center> <%end%>

I want to display the readings according to the selected state for eg: If I select "Karnataka" from the select box i should get all the rows in that table whose state="Karnataka"

My table schema is as below create_table "stations", :force => true do |t|     t.string "name"     t.string "unit_id"     t.integer "sim_no"     t.string "state"     t.string "district"     t.string "province"     t.integer "zipcode"     t.string "latitude"     t.string "longitude" end

Please give me a reply?

I wish to answer your query, but I need to know what you are performing on Readings controller’s index page ? I think you are having a list of all locations irrespective of state selected pre-displayed on index page.

And on selection of a particular state from Stations collection_select you want to recalculate and display list of locations related to only SELECTED STATE… maybe after a page refresh or AJAX operation.

Correct me if I am wrong, then you’ll have the solution.

NRJ

use j query or java scripting to do so or you may saw on event change on tutorial OK

Hai nitin,

What you think is correct.Now i am getting alllocations irrespective of state selected pre-displayed on index page.

I want to display only those "districts" which are part of SELECTED state in collection_select after AJAX operation Please help me...

Thanks in advance

Veena

Good to know,

Because AJAX is involved and you’re new in rails, I am assuming you want to have it work using Prototype library and RJS.

Some pages to let you understand Prototype and JQuery AJAX frameworks in rails…

http://www.slideshare.net/remy.sharp/prototype-jquery-going-from-one-to-the-other http://www.prototypejs.org/

http://wiki.github.com/madrobby/scriptaculous/

  1. Now, things you will need to make AJAX work. If you happen to have a layout file for your application, it should load the javascripts necessary to support AJAX operations.

<%= javascript_include_tag :defaults %> will do it for you. It will include prototype and effects, etc. javascript files.

  1. But if you do not have any layout file like application.rhtml or application.html.erb then you may also choose to put “javascript_include_tag :defaults” line directly on index.html.erb or index.rhtml file under your app/views/readings/index.html.erb or …index.rhtml.

  2. Now we are talking about ReadingsController index action with a list of all locations displayed on it in a div with id say - < div id=“reading_list_div” > You need one more action which will be take one argument as selected state and will populate a new list of locations which are related to THAT PARTICULAR state and render the new list of locations via AJAX on the same index page.

  3. <%= f.collection_select :state,Station.find(:all,:group => “state”,:order =>“state”), :id, :name, {:include_blank =>“None”, :onclick => remote_function(:url =>“/reading/show_readings_for_state”, :with => “‘state_id=’+this.value+'”) } %>

This collection select will trigger an AJAX call to your ReadingsController class for a action named - “show_readings_for_state” which you should define in the controller as,

def show_readings_for_state

@readings = Reading.find_all_by_state_id(params[:state_id]) respond_to do |format| format.js # this will not redirect the page but will look for javascript template named (“show_readings_for_state.rjs” inside the folder “/app/views/readings/”) to execute once #@readings variable is initialized.

end #End of respond_to do block end #End of action show_readings_for_state

  1. Now you will need a partial file to replace old list of readings

say _readings_list.html.erb inside “app/views/readings/_readings_list.html.erb”

  1. And finally the rjs template which will be automatically called after variables in show_readings_for_state action are initialized… File could be - “show_readings_for_state.rjs” inside the folder “/app/views/readings/”

page.replace_html :reading_list_div, :partial=>‘readings_list’ , :object=>@readings page.visual_effect :highlight, ‘reading_list_div’

Maybe this will help you.

Nitin.

Thank u Nithin for your detailed explanation... But i am still not able to do the ajax functionality...

I hav added <%= javascript_include_tag :defaults %> in my "admin" layout

Controller :

class ReadingsController < ApplicationController   layout "admin"

  def index     @time=Time.now     @locations= Station.find(:all,:group => "state",:order =>"state")     @district=Station.find(:all)     @readings= CurrentReading.find(:all)     @range=ConfigureParameter.find(:all)   end

  def show_readings_for_state     @district = Station.find_all_by_state_id(params[:state_id])     respond_to do |format|       format.js    end   end end

readings/index.html.erb

<% form_for :station ,:url => {:action => 'index',:id => @station } do

f> %>

   <center><b> <%= f.label :Stations %></b>

<%= f.collection_select(:state, @locations, :id, :state,{},{:onclick =>remote_function(:url =>"/readings/show_readings_for_state",:with =>"'state_id='+this.value+'")} ) %>

</center> <br/><br/>   <table cellpadding="5">     <tr>         <th>Stations</th>         <th>Time Stamp</th>         <% for sensor_type in @range %>             <th><%= sensor_type.Parameters %></th>         <% end %>     </tr> <div id="reading_list_div">      <% for loc in @district %>        <tr>           <td>               <%=f.radio_button :contact_preference, "0", :tabindex =>loc.id %> <%=h loc.district %>           </td>        </tr>      <%end%> </div> </table> <br/> <%end%>

show_readings_for_state.rjs

page.replace_html :reading_list_div, :partial=>'readings_list' , :object=>@district page.visual_effect :highlight, 'reading_list_div'

_readings_list.html.erb

<% for loc in @district %>     <%= radio_button :contact_preference, "0", :tabindex =>loc.id %> <%=h loc.district %>

     <%end%>

This is the code.please look at it and help me.

Thank you, Veena

Veena Jose wrote:

Thank u Nithin for your detailed explanation... But i am still not able to do the ajax functionality...

I hav added <%= javascript_include_tag :defaults %> in my "admin" layout

Controller :

class ReadingsController < ApplicationController   layout "admin"

  def index     @time=Time.now     @locations= Station.find(:all,:group => "state",:order =>"state")     @district=Station.find(:all)     @readings= CurrentReading.find(:all)     @range=ConfigureParameter.find(:all)   end

end

Hi, If you are using jquery,you can see e.g which is similar

3. Now we are talking about ReadingsController index action with a list of all locations displayed on it in a div with id say - < div id="reading_list_div" > You need one more action which will be take one argument as selected state and will populate a new list of locations which are related to THAT PARTICULAR state and render the new list of locations via AJAX on the same index page.

How can we do this in the controller?I followed all your steps..but in vain... Plz reply me

Thanks , Veena

Do one thing, give me migrations or the sql dump of your database structure, and your app’s directory zip, I will try to replicate the issues you’re having and then can assist you further. Because “not able to do the ajax functionality” is a very big scope of a problem, there can be many reasons.

Hai Nitin,

    I am sending you the app and migrations folder.Please have a look.I know that i am asking too many questions..Sorry 4 troubling you...

Thank you very much....

Veena

Attachments: http://www.ruby-forum.com/attachment/4544/app_n_migrations.rar

Your routes file is missing. And on which page do you need the functionality working… ? (without css it is looking all black and white) Nitin…

Nitin Rajora wrote:

Your routes file is missing. And on which page do you need the functionality working.. ? (without css it is looking all black and white) Nitin..

Hai Nitin,

   I have attached the css and the config directory

Thanks, Veena

Attachments: http://www.ruby-forum.com/attachment/4545/new.rar

Veena Jose wrote:

Nitin Rajora wrote:

Your routes file is missing. And on which page do you need the functionality working.. ? (without css it is looking all black and white) Nitin..

I want the things to be working on the readings/index

Hi Veena,

I wasn’t sure what was the Data you wanted to display on your Readings index page, but I have assumed following situation…

  1. Readings table initially is showing all current_readings for all stations on index page

  2. There is a stations dropdown filled with all the stations inside “stations” table.

  3. When user selects a particular station from the dropdown, the table showing Readings… should be updated for current_readings specific to selected station…

I have added some entries to current_readings table.

Have a look, And AJAX is supposed to work on Readings/index page. I have also added a route in your routes file.

Hope you can change this working AJAX structure according to your requirements

I am attaching two files -

  1. Zip of the dummy app I created to make AJAX work.
  2. sql for the database I used. Nitin

awms_Mar8_2010_Nitin.zip (111 KB)

awms_db_8Mar2010.sql (5.11 KB)

Thanku Thanku Thanku soooooooo much........You have really helped me a lot..... The thing you assumed is right for some extend.

1. Readings table initially is showing all current_readings for all stations on index page

2. There is a stations dropdown filled with all the states in the "stations" table.(eg.,Karnataka,Mumbai etc)

3.When user selects a particular state from the dropdown, the table showing Readings.. should be updated for current_readings specific to selected state...(eg. If i select "karnataka" and i hav 2 stations in Karnataka it should update the readings of those two stations for current_readings)

Your explanations are very easy to understand...Thank you sooooooooo much.

Thanks, Veena

Sincerely, glad you made it work. Nitin.

Hai Nitin,

    I am again disturbing you.....Your code is working fine for me.But i want something like this.

1. Readings table initially is showing all current_readings for all stations on index page

2. There is a stations dropdown filled with all the states in the "stations" table.(eg.,Karnataka,Mumbai etc)

3.When user selects a particular state from the dropdown, the table showing Readings.. should be updated for current_readings specific to selected state...(eg. If i select "karnataka" and i hav 2 stations in Karnataka it should update the readings of those two stations for current_readings)

When i got your code i thought i can make the change very easily.I wasted this whole morning in doing this.I don't know where i am going wrong.

Can you please help me out.....

Thanks, Veena

Hi Veena,

It is simple logic… Instead of finding CurrentReadings by station_id, you need to find CurrentReadings for stations_id for which stations.state==SELECTED_STATE_VALUE

Files are attached have a look.

Nitin.

awms_with_states_9Mar2010.zip (118 KB)

awms_db_9Mar2010.sql (5.14 KB)

Thank you sooooooooooooooooo much....

I was stuck with some syntax error.

Thanks Once Again....

Sincerely, Veena

Hai Nitin,

    You have helped me a lot...... Can you please suggest me some good books or links where a newbie like me can understand Ruby on rails much better....

Thank you once again.........

Sincerely, Veena