{"action":"opened","issue":{"url":"https://api.github.com/repos/rails/rails/issues/22814","labels_url":"https://api.github.com/repos/rails/rails/issues/22814/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/22814/comments","events_url":"https://api.github.com/repos/rails/rails/issues/22814/events","html_url":"https://github.com/rails/rails/issues/22814","id":124107378,"number":22814,"title":"Routes constraints should be enforced when generating urls","user":{"login":"fabn","id":324213,"avatar_url":"https://avatars.githubusercontent.com/u/324213?v=3","gravatar_id":"","url":"https://api.github.com/users/fabn","html_url":"https://github.com/fabn","followers_url":"https://api.github.com/users/fabn/followers","following_url":"https://api.github.com/users/fabn/following{/other_user}","gists_url":"https://api.github.com/users/fabn/gists{/gist_id}","starred_url":"https://api.github.com/users/fabn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabn/subscriptions","organizations_url":"https://api.github.com/users/fabn/orgs","repos_url":"https://api.github.com/users/fabn/repos","events_url":"https://api.github.com/users/fabn/events{/privacy}","received_events_url":"https://api.github.com/users/fabn/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-12-28T18:54:33Z","updated_at":"2015-12-28T18:54:33Z","closed_at":null,"body":"I've encountered this issue other times, but now I'm in a different context thus I'm opening this issue.\r\n\r\nSuppose I have the following routes in my app:\r\n\r\n```ruby\r\nRails.application.routes.draw do\r\n  constraints(host: 'foo.example.com') do # or subdomain: 'admin'\r\n    resources :posts\r\n  end\r\n  resources :users # respond to all domains.\r\n  # ...\r\nend\r\n```\r\n\r\nand the app is accessible to both `foo.example.com` and `bar.example.com`.\r\n\r\nWhen I access http://bar.example.com/users in I'd expect that all generated url for posts use 'foo.example.com' e.g. `post_url(1)` should always return http://foo.example.com/posts/1.\r\n\r\nThe constraint should be enforced also if one uses `post_path(1)` from a domain different than `bar.example.com` by raising an exception or by transparently convert it to `post_url`.\r\n\r\nThe same behavior should be present when using subdomain constraints.\r\n\r\nIn my current use case I'm experiencing the same issue described in [this question](http://stackoverflow.com/q/28479127/518204). I'm doing something similar to this:\r\n\r\n```ruby\r\nRails.application.routes.draw do\r\n  constraints host: ENV['ADMIN_HOST'] do\r\n    mount Admin::Engine, :at => '/'\r\n  end\r\n  # main app routes\r\n  # ...\r\nend\r\n```\r\n\r\nand I'm not able to write links in application that points to the engine unless I'm explicit with\r\n\r\n```erb\r\n<%= link_to 'Admin App', admin.root_path(host: ENV['ADMIN_HOST']) %>\r\n```\r\n\r\nI tried to add this line but it only works outside views\r\n```ruby\r\nAdmin::Engine.routes.default_url_options[:host] = ENV['ADMIN_HOST']\r\nAdmin::Engine.routes.url_helpers.root_url #=> \"http://#{ENV['ADMIN_HOST']}\"\r\n```\r\n\r\nI've traced the `admin.root_url` call to [this line in RoutesProxy](https://github.com/rails/rails/blob/7f18ea14c893cb5c9f04d4fda9661126758332b5/actionpack/lib/action_dispatch/routing/routes_proxy.rb#L31) and the issue is that `url_options` hash is populated with [request parameters](https://github.com/rails/rails/blob/7f18ea14c893cb5c9f04d4fda9661126758332b5/actionpack/lib/action_controller/metal/url_for.rb#L34) and finally overridden by [ActionDispatch::Routing::UrlFor#url_options](https://github.com/rails/rails/blob/7f18ea14c893cb5c9f04d4fda9661126758332b5/actionpack/lib/action_dispatch/routing/url_for.rb#L112) that ultimately call current controller `default_url_options`.\r\n\r\nSo I think the above issue can only be solved by changing router behavior to enforce host and subdomain constraints.\r\n\r\n"}}