Portal Labs, LLC Logo
Custom Web Design and Application Development

Fixing subdomain_fu With Named Routes

subdomain_fu is a great little plugin for rails to make managing multiple subdomains easier.  Watch the railscast and try it out for yourself.

Unfortunately, it didn’t handle named routes with the :subdomain parameter correctly.  You can learn more about this bug over at the subdomain_fu lighthouse bug tracker.  The ticket has been open for several months now with no hint at a solution coming anytime soon.

The fix is pretty simple.  Kudos go to ticket #4 for the solution.  Open up vendor/plugins/subdomain-fu/lib/subdomain_fu/url_rewriter.rb and add the following to the bottom of the ActionController module:


module Routing
    module Optimisation
      class PositionalArgumentsWithAdditionalParams
        def guard_condition_with_subdomains
          # don't allow optimisation if a subdomain is present - fixes a problem
          # with the subdomain appearing in the query instead of being rewritten
          # see http://mbleigh.lighthouseapp.com/projects/13148/tickets/8-improper-generated-urls-with-named-routes-for-a-singular-resource
          guard_condition_without_subdomains + " && !args.last.has_key?(:subdomain)"
        end

        alias_method_chain :guard_condition, :subdomains
      end
    end
  end

Restart your rails server and everything should start to work correctly for you.

Tags: , ,

3 Responses to “Fixing subdomain_fu With Named Routes”

  1. Portal Labs | Custom Web Application Development Says:

    [...] recently upgraded our project that was using subdomain_fu to rails 2.2 and the previous fix no longer [...]

  2. gaskell.org » undefined method ‘guard_condition’ when using subdomain_fu on Rails 2.2 Says:

    [...] eventually figured out that this code was added a couple months ago - I removed the code referenced in the blog post and now my routing [...]

  3. دردشة Says:

    Thank you my brother

Leave a Reply

Client DropBox   |   Client Login