FattRyan
FattRyan

Reputation: 896

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

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

Upvotes: 1

Views: 1605

Answers (1)

Gwilym Kuiper
Gwilym Kuiper

Reputation: 139

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.

Upvotes: 2

Related Questions