0

I created a simple application with a news module and defined the news as a separate mountable engine (it will be used on other projects too). I need to have the ability to mount the admin and user parts of the engine as separate routes on parent application. Now I can mount the whole engine as

Rails.application.routes.draw do
     mount Jnews::Engine => "/news"
end

but I want to separate admin and user routes on parent app as /news for user and /admin/news for admins. Is this possible?

SethO
  • 2,703
  • 5
  • 28
  • 38
javidan
  • 11
  • 3

1 Answers1

0

I think it depends on why you want to do this:

  1. If you want to keep users and admins as separate code in the news gem - then you probably want to isolate 2 namespaces, and then mount each of those - which might involve having them as separate engines themselves.

  2. If what you want is (and this is what I think you mean?) is to have the code shared in the news engine, but accessed at 2 different url's in the main application based on whether the user is a plain user or an admin? What I would do in that case is something like this:

in the main application

Rails.application.routes.draw do
   mount Jnews::Engine => "/app"
end

in the engine routes

Jnews::Engine.routes.draw do
    match "/news", :to => "some_controller#some_action"
    match "/admin/news", :to => "some_controller#some_action"
end

And then in the main application, based on what the user is (user or admin) you can redirect to either app/news or app/admin/news

I hope that helps, I am not really sure about doing conditional routing.

However, here is a really good guide for routing: http://guides.rubyonrails.org/routing.html

You could perhaps try something along the lines of mounting an engine with dynamic routing?

mount Jnews::Engine => ":user_id/news"

where you end up with Jnew::Engine routed at either user/news and admin/news? I have never actually done that, so I don't know if it's possible, however... maybe?

Let me know if any of this helps :) Cheers

Krista
  • 895
  • 1
  • 6
  • 16