9

I am making a custom admin panel in a namespace "admin".

I have resources "courses" within that namespace.

But I would also like a route to "courses" that is not in that namespace:

eg: BOTH localhost:3000/admin/courses AND localhost:3000/courses

It's OK if this requires different controllers.

My concern is that its not really DRY if i have both resources for the same route.

namespace admin do
   resources :courses
end

and just

resources :courses

Is there a way to have one resource be shared between namespace and without namespace, or is the example above the way to go?

Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82
  • After clarification, this might be a duplicate of http://stackoverflow.com/questions/20492370/rails-devise-same-resource-different-controller-based-on-user-type – jkeuhlen Jul 30 '15 at 14:17
  • Are you saying that both routes should go to the same pages? Or is the action different for admin? – Daiku Jul 30 '15 at 14:20
  • different pages,different controlllers,maybe same actions, and i wanted same routes if possible with same line of code. people below answered me :) – Petros Kyriakou Jul 30 '15 at 14:22
  • I don't think it's a duplicate. If it were, it would be a duplicate of [this one instead](http://stackoverflow.com/questions/24695096/rails-routing-add-shallow-concern-to-root), however the question is slightly different. The author here is looking for a way to refactor to DRY things up. However maybe the question name could be rephrased ? – Cyril Duchon-Doris Jul 30 '15 at 16:37
  • What would be an appropriate title for this then? – Petros Kyriakou Jul 30 '15 at 16:46

2 Answers2

7

Oh wait ! There's also the possibility to use concerns !

concern :shared_actions do
   resources :courses
   resources :something_else
end


namespace :admin do
   concerns :shared_actions
end
concerns :shared_actions # Will add it to the root namespace ^^

EDIT : apparently this is what this guy also tried to do :D

Community
  • 1
  • 1
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
6

I'm not really sure I understand what you mean, but

namespace :something is actually a shorthand for scope :something, module: :something, as: :something

  • scope :something will add /something/ as a URL prefix
  • scope module: :something will add /something as a controller prefix (controllers will be fetched under controlelrs/something/the_controller.rb
  • scope as: :something will add the something as a prefix for path helpers

Now it's totally fine to have both in your routes

resources :courses
# Will generate "/courses/", "/courses/new", "/courses/1/edit", ...
# And will point to `controllers/courses_controller.rb`

namespace :admin do
  resources :courses
end
# Will generate "/admin/courses/", "/admin/courses/new", "/admin/courses/1/edit", ...
# And will point to `controllers/admin/courses_controller.rb`

Does this answer your question ?

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • I agree that it is fine to have both in the routes, but isn't it cleaner to simply specify the scope and module? Which is exactly how the rails guide suggests to do it (at least to my understanding). – jkeuhlen Jul 30 '15 at 14:10
  • as i said jkeuhlen,i think you misunderstood what i want to do, i want to have a separate admin controller for admin/courses/ path and another controller for the front-end path /courses. with the syntax you guys give me all it does is point to admin/courses controller which is not want i want. I just asked if there is special syntax in routes.rb that can yield me routes for courses/ path and admin/courses/ path with one line of code instead of writing resources two time. understand now? is it possible? – Petros Kyriakou Jul 30 '15 at 14:12
  • I believe it depends on your personal taste. Personally, when I read `namespace :admin` it makes more sense to my mind than just reading `scope :admin, module: :admin, as: :asmin` – Cyril Duchon-Doris Jul 30 '15 at 14:12
  • @jkeuhlen What the solution you are suggesting will do is create `courses_path` instead of `admin_courses_path` and will take it to admin controller always. Instead the OP needs to map to admin and a without scoped controller both using a single resource. – Deepesh Jul 30 '15 at 14:13
  • @CyrilDD Fair enough. Just curious! I'm here to learn as well :) – jkeuhlen Jul 30 '15 at 14:14
  • @PetrosKyriakou No it is not possible with just one line of code to handle both `/admin` and `/`. but the above code does exactly what you want., it doesn not point JUST add `/admin/resources` but also `/resources`. check the routes `rake:routes` – Cyril Duchon-Doris Jul 30 '15 at 14:14
  • @Deep Yeah, I misunderstood the question. Asked OP to update to clarify and I'll remove mine momentarily since it isn't helpful. – jkeuhlen Jul 30 '15 at 14:14
  • @PetrosKyriakou I believe there is no short way of doing that you will need to keep both the resources in your routes file. And it is necessary too to have that. Like in my application I have admin and frontend. And I have a slug attribute in courses. So in the admin side I have route like `/courses/1` and in front end I have `/courses/english-123245`, not displaying the `id` to frontend users instead displaying slug. So keeping the seperate helps in tasks like this. – Deepesh Jul 30 '15 at 14:17
  • thank you guys i believe you answered me what i wanted to know!thanks again! – Petros Kyriakou Jul 30 '15 at 14:18
  • @CyrilDD what is your thought of the DRY paradigm in this case? i mean obviously its duplicate code just in one case its under a namespace. syntax like resources :courses, namespaces: '/' , 'admin' ,wouldnt be a better approach without duplicating code? – Petros Kyriakou Jul 30 '15 at 14:23
  • Just reeeeeembered about concerns. See my other answer ! – Cyril Duchon-Doris Jul 30 '15 at 14:24