Rails - Drop down menu to execute actions in the controller?

泪湿孤枕 提交于 2019-12-06 14:40:32

问题


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".


回答1:


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}

Then in the view have:

select_tag :create-model, options_for_select(@urls)

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.



来源:https://stackoverflow.com/questions/5602169/rails-drop-down-menu-to-execute-actions-in-the-controller

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