I have controllers that work “in context” of some model. Example: controllers UsersController and InvoicesController can work in context of Company model – they show users and invoices for some company. I use nested resources as routing solution and have URLs like
http://localhost:3000/companies/8/users
What is the easiest and flexible way to make links on index view, that will reflect on context?
It means that
new_user_path or
new_company_user_path
will be created depending on context. I don’t want to use a lot of if…then’s.
Is there any solution?
You won’t get away without ANY if switches, but you can write a helper to at least keep it out of your view code.
I would suggest you look at the
url_forhelper in Rails and use that inside a helper like this:Something like this, you can get as fancy as you want with your helper