문제

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

도움이 되었습니까?

해결책

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

다른 팁

you can do:

constraints subdomain: 'admin' do
  namespace :admin do
    # ...
  end
end

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top