Rails - 下拉菜单来执行控制器中的操作? [英] Rails - Drop down menu to execute actions in the controller?

查看:170
本文介绍了Rails - 下拉菜单来执行控制器中的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确地表达了这个问题,但是如何创建一个下拉菜单,其中有一些可以在某个模型上创建新条目的选项?比方说,我有用户,费用和预算,在主页面上,我想通过从下拉菜单中选择该选项来添加新用户,新的费用或新的预算;而不是单击一个链接,将我带到new_user_path,new_expense_path或new_budget_path。

I'm not sure I phrased the question correctly, but how would I create a drop down menu with options to create a new entry on a certain model? Let's say for example I have User, Expense, and Budget, and on the main page I want the option to add a new user, new expense, or new budget by selecting that option from a drop down; instead of clicking a link that takes me to "new_user_path", "new_expense_path", or "new_budget_path".

推荐答案

使用jQuery,这是我会做的:

有一个哈希定义如下:

If you don't mind using jQuery, this is what I would do:
Have a hash defined something like:

@urls = {"Create New USer..." => new_user_url, "Create new expense", => new_expense_path}

然后在视图中有:

select_tag :create-model, options_for_select(@urls)

然后在application.js或类似的东西

Then in application.js or something like that

$('#create-model').change(function() {
  window.location = $(this).find('option:selected').val();
});

我没有尝试过,但它的工作没有太多的编辑。

I haven't tried it but it should work without too much editing.

这篇关于Rails - 下拉菜单来执行控制器中的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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