使用下拉列表和Rails中的jQuery重定向到编辑路径 [英] Redirect to edit path using drop down and Jquery in rails

查看:71
本文介绍了使用下拉列表和Rails中的jQuery重定向到编辑路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有选择的项目下拉列表.当用户选择和item时,我需要将item_id重定向到edit_item_path.例如如果用户选择ID为= 5的商品,则需要重定向到商品/5/编辑路径

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

如何在Rails 3和jquery中做到这一点?

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>

在这种情况下,以下jquery片段应该可以工作:

in that case following jquery snippet should works:

$('#editable_pages').change(function() {
    window.location = "items/+ $(this).find(":selected").text() +/edit";
});

Nicer解决方案是使用Rails路由助手.为此,您可以在每个选项中添加带有url的html5数据属性,例如:

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

这篇关于使用下拉列表和Rails中的jQuery重定向到编辑路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆