Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 subdomain routing and URL helpers
    primarykey
    data
    text
    <p>I am running into routing issues regarding subdomains in Rails 3 when porting from Rails 2.3.x + gem subdomain_routes. With the subdomain_routes gem I was able to map routes easily by model like the following:</p> <pre><code># config/routes.rb map.subdomain :model =&gt; :site do |site| resources :pages end </code></pre> <p>This would generate url helpers, like <code>site_pages_url</code> and could be used like this:</p> <pre><code># console @site = Site.find_by_subdomain(“yehuda”) app.site_pages_url(@site) =&gt; http://yehuda.example.com/pages app.site_page_url(@site, @page) =&gt; http://yehuda.example.com/page/routes-rock </code></pre> <p>In Rails 3 this roughly would translate to:</p> <pre><code># config/routes.rb class SiteSubdomain def self.matches?(request) request.subdomain.present? &amp;&amp; request.subdomain != 'www' &amp;&amp; request.params[:site_id].present? end end Blog::Application.routes.draw do resources :sites do constraints(SiteSubdomain) do resources :pages end end end </code></pre> <p>and overloading the standard url_for should basically work like in subdomain_routes:</p> <pre><code>module UrlFor def with_subdomain(subdomain) subdomain = (subdomain || "") subdomain += "." unless subdomain.empty? [subdomain, request.domain, request.port_string].join end def url_for(options = nil) if options.kind_of?(Hash) &amp;&amp; options.has_key?(:subdomain) options[:host] = with_subdomain(options.delete(:subdomain)) end super end end ActionDispatch::Routing::UrlFor.send(:include, UrlFor) </code></pre> <p>However, the url helpers still do not generate the correct URL like <code>site_pages_url(@site) #=&gt; <a href="http://www.example.com/pages" rel="nofollow">http://www.example.com/pages</a></code>, instead of the expected <code>http://yehuda.example.com/pages</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload