Rails: allow singular namespace and resource to share a view folder
-
14-11-2019 - |
Frage
I have the following routes defining certain resources:
resource :account, :only => [:show]
namespace :account do
resource :billing
end
So, I have an AccountsController which generates the "show" page at /account
.
I also have a BillingsController which I want to be viewed at /account/billing
.
This is working fine, but one thing that's bugging me is the convention says the view folder for the AccountsController is plural even though its a singular resource -- not a big deal, but when creating a matching namespace for the nested resource I now have two seperate view folders -- /app/views/account
(for namespace) and app/views/accounts
(for account resource).
So, this kind of throws me off.
What would be the best way to make the AccountsController use the singular account
folder for views?
Lösung
For what it's worth, I decided it would be easiest to change AccountsController
to AccountController
and change my routes.rb
file like so:
resource :account, :controller => 'account'
That was a little cleaner than anything I could figure out.
Andere Tipps
There are a bunch of options, but using self.prepend_view_path("views/account")
in a method called by a before_filter in the AccountsController should work. It will force it to look in the specified directory for a view, before checking the default.