Reputation: 459
The drop down list elements that I want to click on are
<select class="store-select">
<option value="" selected="selected">Please select</option>
<option value="1">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
And I want it to trigger an action in the controller based on the option that was clicked. So one of the things I'm hoping to learn is how to access that value in the controller. I also want to redirect the page after an option is clicked. I'm able to redirect the page when other links are clicked in my view, but when I click on an element in the drop down list, it doesn't redirect. It doesn't even go to the view for that method. When an element in the drop down list is clicked it just stays on the current page.
def practice # the redirect doesn't work in this method but works in others
# redirect_to "https://tosbourn.com"
render "ibotta/show"
end
My application.js file looks as the following
$('.store-select').on('change', function() {
$.ajax({
url : '/users',
type : 'GET',
data : { user_id: this.value },
dataType:'script'
});
})
My routes.rb file contains the following
get 'users' => 'smiths#practice'
Let me know if there is anything I'm missing that you want to know about this problem. Thank you.
Upvotes: 0
Views: 464
Reputation: 6531
if you are using ruby on rails then you can do this one short line for example: -
<%your_options = [['Please select', ''],['Administrator','1'],['User','2'],['Editor','3']]%>
<%= select_tag "store-selects", options_for_select(your_options),:onchange => "self.location='/users/'+this.options[this.selectedIndex].value", class: "your_class" %>
and your routes should be
get 'users/:id' => 'smiths#practice'
thank you.
Upvotes: 1
Reputation: 4298
As you want to redirect on drop down change you can follow this
Suppose your drop down is
<select name="select1" id="select1">
<option value="0">Page1</option>
<option value="1">Page2</option>
<option value="2">Page3</option>
<option value="3">Page4</option>
</select>
and your java script change event is
$select1.on( 'change', function() {
switch(this.value){
case '0':
window.location = [page1url];
break;
case '1':
window.location = [page2url];
break;
case '2':
window.location = [page3url];
break;
case '3':
window.location = [page4url];
break;
}
});
you will redirect to urls you want on change
Upvotes: 2