Redirect to edit path using drop down and Jquery in rails

久未见 提交于 2020-01-02 23:12:44

问题


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?


回答1:


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



回答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>


来源:https://stackoverflow.com/questions/12413781/redirect-to-edit-path-using-drop-down-and-jquery-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!