I am adding an admin namespace with a few controllers, so our stuff can edit contents from the backend.
For example, I have a Book model, and a books_controller allowing guests browse. Now I am adding an admin/books_controller, providing a backend for admins to edit books.
namespace :admin do
resources :books, except: :show
end
resources: books, only: [:index, :show]
Currently for all the link_to and form_for in the admin_books_controller views, I need to add the namespace :admin like this:
<%= form_for([:admin, @post]) do |f| %>
(in the normal controller it would be form_for(@post))
It gets a bit repetitive, and I am wondering if I can set it somewhere in the admin controller so all the link_to within that controller will route in the admin namespace?
If you scope the model under the namespace, that should automatically be reflected in the form generator.
So something like
rails g model admin/bookwill generate a book model nested under the admin namespace.If you then build a form atop that object, something like
form_for Admin::Book.newthis should target any appropriately nested routes, such as:I hope this gives some indication of a direction that you can try out.
Best regards.