I have a select drop down of items. When the user selects and item I need to redirect to edit_item_path for the item_id. e.g. If user selects and item with id =5 I need to redirect to items/5/edit path
How to do this in rails 3 and jquery?
Lets say that your dropdown looks like this:
<select id="editable_pages">
<option value="1">Edit 1</option>
<option value="2">Edit 2</option>
</select>
in that case following jquery snippet should works:
$('#editable_pages').change(function() {
window.location = "items/+ $(this).find(":selected").text() +/edit";
});
Nicer solution is to use rails route helpers. To do that you could add a html5 data attribute with url to each options, eg:
<option value="1" data-edit-url="<%= edit_item_url(1) %>">Edit 1</option>
$('#editable_pages').change(function() {
window.location = $(this).find(":selected").data('edit-url');
});
I would do something like this...
<% @item_paths = {""=>"", "Item 5" => edit_item_path(5), "Item 4" => edit_item_path(4)}%>
<%= select_tag :edit_item, options_for_select(@item_paths)%>
<script type="text/javascript">
$('#edit_item').change(function() {
window.location = $(this).find('option:selected').val();
});
</script>