1

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?

2 Answers 2

3

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');
});
0
2

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>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.