Can I set the default namespace route in the controller
I am adding an admin namespace with multiple controllers so our stuff can edit content from the backend.
For example, I have a model Book
and books_controller
that allows visitors to browse. Now I add admin/books_controller
by providing support for admins to edit books.
namespace :admin do
resources :books, except: :show
end
resources: books, only: [:index, :show]
Currently for everyone link_to
and form_for
in admin_books_controller views I need to add a namespace :admin
like this:
<%= form_for([:admin, @post]) do |f| %>
(in a normal controller it would be form_for(@post)
)
It gets a little repetitive and I'm wondering if I can set it up somewhere in an admin controller so that everything link_to
inside that controller is directed to the admin namespace?
If you span the model under the namespace, this should be automatically reflected in the form generator.
So something like rails g model admin/book
would create a book model nested in the admin namespace.
If you then build a form on top of that object, something like form_for Admin::Book.new
should target any nested routes that fit, like this:
namespace :admin do
resources :books
end
Hopefully this will provide some direction for you to try.
Sincerely.