W
W
Weziw2015-01-28 23:03:57
Ruby on Rails
Weziw, 2015-01-28 23:03:57

How to organize namespaces in Rails?

Hey! How to organize work with namespaces in rails? The simplest example is I have a guest page. When a guest goes to the user group, then from / it moves to /dashboard
There I have some controllers. For example, post controller. I also have a Post model (which, due to the presence of the admin in the /admin namespace, should not be moved to Dashboard::Post).
As a result, we get the following routing:

root 'guest#index'

namespace :dashboard do
  resources :posts
end
namespace :admin do
  resources :users, :posts
end

And here it would be desirable to receive ways from models correctly. For example, if we are in the controller, which is in the /dashboard namespace, then it form_for @postshould refer to /dashboard/posts/new, and if we are in the /admin namespace, then to /admin/posts/new
How can we achieve this behavior? Or am I building the wrong architecture? Community Help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
caution, 2015-01-30
@caution

look at what I FOUND by writing in Google: "namespace rails"
rusrails.ru/rails-routing#prostranstvo-imen-kontro... it's
amazing what possibilities the keyboard gives

J
Jeiwan, 2015-01-28
@Jeiwan

So if you have different namespaces, then the views will be taken from different folders: for the dashboard - from views/dashboard, and for the admin - from views/admin. So there shouldn't be any problems.
And if you mix views, and load the same views in different namespaces, then you should think about whether you need to do this. At some point, one of the namespaces will need to make its own representations.

V
vsuhachev, 2015-01-29
@vsuhachev

One option I've seen is to create the Dashboard::Post and Admin::Post classes to inherit from Post. I didn’t use this myself, perhaps this solution has some weaknesses, since it is not very common.
I solved a similar problem by explicitly specifying the namespaceform_for [:admin, @post]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question