0

Not sure if my topic title is correct, but here is my question

I have namespace called :admin, so it looks like mysite.com/admin. In this section i have some links, that pointing to controllers inside this namespace. But since we have subdomain admin, and my namespace :admin as well, i'd like to all links that are being generated by routes.rb to prepend string admin., so the link would look like admin.mysite.com/admin/some_other_path

I've tried to add constraints to routes.rb, but that didn't work for me

Avdept
  • 2,261
  • 2
  • 26
  • 48

3 Answers3

1

But since we have subdomain admin, and my namespace :admin as well, i'd like to all links that are being generated by routes.rb to prepend string admin.


Routes

In your routes, you should have this:

constraints({ subdomain: "admin" }) do
    namespace :admin do
        # routes here
    end
end

If you wanted to have no path for your admin namespace (I.E admin.domain.com/some_other_path), you can do this:

constraints({ subdomain: "admin" }) do
    namespace :admin, path: "" do
        # routes here
    end
end

--

URL

When using URLs, you have to use the _url helpers (not _path). We literally just discovered this yesterday - the _path helpers only work to append relative paths to your url; the _url gives you a totally fresh url

This means if you have a route as follows:

admin_root_path "admin/application#index, constraints => {subdomain: "admin"}

You'll call this with this route helper:

<%= link_to "Admin", admin_root_url %>

This will prepend the required subdomain for you when calling links etc

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

Define routes in routes.rb under admin namespace like this

namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
  constraints(Subdomain) do
    # your routes
  end
end

Routes defined under this block will always come under admin in link, e.g., /admin/some_other_path

To add subdomain to the admin namespace take a look at this question

Rails namespace admin on custom subdomain

Community
  • 1
  • 1
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • thats what i have at the moment. But i want all links under this namepspace to have subdomain `admin` – Avdept May 12 '14 at 09:00
0

you can do:

constraints subdomain: 'admin' do
  namespace :admin do
    # ...
  end
end
BroiSatse
  • 44,031
  • 8
  • 61
  • 86