Last active
August 29, 2015 14:02
-
-
Save beneggett/156d2ab2947fc4c28ffc to your computer and use it in GitHub Desktop.
Routing Question by ThirtySharp762 (http://www.reddit.com/r/rubyonrails/comments/28wsoy/routing_question/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
<%= domain_stylesheet_link_tag "application", media: "all" %> | |
<%= domain_javascript_include_tag "application" %> | |
#... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
###... your existing code | |
before_action :find_domain | |
private | |
def find_domain | |
@domain = request.host | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DomainAssetHelper | |
def domain_image_tag(resource) | |
image_tag("#{@domain}/#{resource}") | |
end | |
def domain_image_path(resource) | |
image_path("#{@domain}/#{resource}") | |
end | |
def domain_stylesheet_link_tag resource, media: "all" | |
stylesheet_link_tag "#{@domain}/#{resource}", media: media | |
end | |
def domain_javascript_include_tag resource | |
javascript_include_tag "#{@domain}/#{resource}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This might live in lib/ folder or wherever you want | |
class DomainConstraint | |
def initialize(domain) | |
@domains = [domain].flatten | |
# you could customize this logic | |
end | |
def matches?(request) | |
@domains.include? request.host | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> This is my cool page that's shared across domains</p> | |
<p> I can load images in a few ways using the normal asset pipeline. </p> | |
<ul> | |
<li> | |
Using the normal image helpers <%= image_tag("mydomain.com/cool.gif") %>. However this isn't really reusable | |
</li> | |
<li> | |
Using the normal image helpers with my @domain set in application controller <%= image_tag("#{@domain}/cool.gif") %> | |
This will automatically grab the domain and search for it in the assets pipeline. | |
</li> | |
<li> | |
Using a helper method like seen above to clean up these image tags: <%= domain_image_tag("cool.gif") %>. | |
This will automatically grab the domain and keeps your views more pretty. | |
</li> | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyApp::Application.routes.draw do | |
# Use a domain_constraint class where you can customize logic for multiple domains, etc. | |
constraints DomainConstraint.new('mydomain.com') do | |
root to: 'my_domain#index' | |
# The rest of your routes for a specific domain could go here | |
end | |
# Simpler constraint for single domain mapping/routing | |
root to: 'your_domain#index', constraints: { domain: "yourdomain.com" } | |
# You could set a default route in the event your domain doesn't match the domains you've mapped above | |
root to: 'main#index' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment