0

How I can create a drop down list using select_tag "xyz", options_for_select or any way where on selecting any option from down list and hitting button will redirect me.

Any help??

<td> 
  <%= select_tag "options", options_for_select( 
                              [["Dashboard", 
                                "/homes/"+registrar.enrollment_application.id.to_s
                              ]] ) %>
</td> 
<input type="button" value="Select" onclick = "check_options()" > 

if above code is right then all i need to write a javascript?? please let me now

function = check_options() { 
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
user588324
  • 310
  • 1
  • 6
  • 19

2 Answers2

0

If you can use javascript or even jquery on the client side you could set the window.location based on the selected item in the drop down, after you click the button:

HTML: This is how you set up your select list and a button

<select id="options">
  <option value="http://stackoverflow.com">Stack Overflow</option>
  <option value="http://google.com/">Google</option>
</select>
<input type="button" id="optionsbutton" value="Go" />

JavaScript: This is how you wire up the button click to the window redirect

var onButtonClick = function() {
    window.location.replace($("#options").val()) //<--- This gets the selected value
                                                 //     and redirects the window
}

$(document).ready(function(){
    $("#optionsbutton").click(onButtonClick);  //<--- This hooks up the button click
                                               //     to do the onButtonClick function
});

See How to redirect to another webpage in JavaScript/jQuery? for more details.

Update Using Provided Code

You'd just have to make your check_options function find the selected value and set the window location:

var check_options = function() {
  window.location.replace($(" [ use selector for select element ] ").val());
});
Community
  • 1
  • 1
John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • I am new to ruby and javascript, sorry if I don't understand properly. Here is the code <%= select_tag "options", options_for_select( [["Dashboard", "/homes/"+registrar.enrollment_application.id.to_s]] ) %> if above code is right then all i need to write a javascript?? please let me now function = check_options() { – user588324 Feb 26 '11 at 00:21
  • You can put the details of your question in the question rather than in comments. I copied your code here into the question, but you can do it yourself too. – John Weldon Feb 26 '11 at 00:35
0

You should add javascript like this

function check_options() { 
  window.location = document.getElementById("options").value
}
rubyprince
  • 17,559
  • 11
  • 64
  • 104