Search code examples
ruby-on-railsrubyruby-on-rails-4rails-engines

Rails Routes Constraints with www is working, domain without www not working


Through another Rails-Engine project, I was able to work with constraints better. I tried to take my previous work and modify it to this one.

I have an app that runs off subdomains, www/admin is restricted but everything else a customer can use. I used the much simpler:

constraint => "www"

Since the above method wouldn't work on a URL without www and just example.com, I was working with the below script:

I put the file in app/constraints/frontend_router.rb

    class FrontRouter
        def self.matches?(request)
            request.subdomain.present? && request.subdomain != "www" && request.subdomain != ENV['DEPLOYED_DOMAIN']
        end
    end

At the top of the routes file, add the required file. The www seems to work but the example.com (without www), was not.

require 'frontend_router'

constraints(FrontRouter) do
    # Landing Page
    scope module: "website" do
        root 'page#index'
    end
end

Solution

  • The plugin I'm using is routing everything. It could've overwritten what I was trying to do. But I found this to be my best personal option: is working with the welcome and the separated dashboard views on the engine.

     gem 'mtwarden', '~> 2.2'
    

    If anybody else has a better solution, I'm willing to try it out and test. But this will accomplish what I need done.