My app allows people to create portfolios. I would like for them to be able to connect their domain to their portfolio.
So somedomain.com would show /portfolio/12, someotherdomain.com would show /portfolio/13 and so on. But I don't want the redirect. I want the user to see somedomain.com in the browser url.
How do I do that?
Ok, I've found this solution:
match "/" => "portfolio#show",
:constraints => { :domain => "somedomain.com" },
:defaults => { :id => '1' }
As I don't have many custom domains, this is fine for now but the question is - how to make this dynamic, to read domain and id data from db?
Ok, let's assume you own yourdomain.com
and use it as your home page for your application. And any other domain name like somedomain.net
is mapped to a portfolio page.
First of all, in your routes.rb
you need to catch yourdomain.com
and map it to wherever your home page is, so that it stands out from the rest of the crowd.
root :to => "static#home", :constraints => { :domain => "yourdomain.com" }
Then you need to catch any other root on any domain and forward it to your PortfoliosController
root :to => "portfolios#show"
Keep in mind that this line will only be checked if the previous line fails to match.
Then in your PortfoliosController
find the requested portfolio by its domain rather than id.
def show
@portfolio = Portfolio.find_by_domain(request.host)
…
end
Of course you may want to rescue from an ActiveRecord::RecordNotFound
exception in case the domain is not in your database, but let's leave that for another discussion.
Hope this helps.
First, you should add a field to the portfolio model to hold the user's domain. Make sure this field is unique. Adding an index to the field in your database would also be wise.
Second, set your root to route to the portfolios#show
action, as you already did, but without the constraints.
Then, in the PortfoliosController#show
method, do the following check:
if params[:id]
@portfolio = Portfolio.find(params[:id])
else
@portfolio = Portfolio.find_by_domain(request.host)
end
After this, the only thing left to do is to make sure your own domain does not trigger the portfolio#show
action. This can be done with the constraint you used before, but now with your own domain. Be sure to put this line in routes.rb above the line for the portfolio#show
action, since the priority is based upon order of creation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With