{"id":5,"date":"2007-09-18T18:48:21","date_gmt":"2007-09-18T23:48:21","guid":{"rendered":"http:\/\/unixmonkey.net\/blog\/?p=5"},"modified":"2007-09-18T18:48:21","modified_gmt":"2007-09-18T23:48:21","slug":"skinning-your-entire-rails-app","status":"publish","type":"post","link":"https:\/\/unixmonkey.net\/?p=5","title":{"rendered":"Skinning your entire Rails app"},"content":{"rendered":"<p>Say I want to deploy my app as a service to several good customers, but I want my app to share the exact layout as each customer site, so it appears to be an internal app to their company and its own clients. I take thier CSS and HTML markup, and make it into a template to wrap around my app&#8217;s content.<\/p>\n<p>When I took to task to replicate that functionality in Ruby on Rails, I wasn&#8217;t certain would be easily accomplished. All the tutorials I&#8217;ve read list template switching as purely CSS affairs. Using CSS to change the presentation of your site is a fine thing, but isn&#8217;t enough if you are looking to completely copy the look and feel of a client&#8217;s website with your app dropped in.<\/p>\n<p>It took some thought and a little help from the community.<\/p>\n<p>Previously, the system I was used to accepted a site variable passed with the login form that told which template to render. like http:\/\/myapp.com\/?site=ibm<\/p>\n<p>That&#8217;s ugly and really obvious to what it is doing, but what other way is there to know which template to render?<\/p>\n<p>I recall setting up accounts at a handful of sites that were in the format of: http:\/\/myusername.myapp.com<\/p>\n<p>This is known as using a subdomain as an account key. Luckily, there is a very simple plugin to set that up in Rails <a href=\"http:\/\/wiki.rubyonrails.org\/rails\/pages\/HowToUseSubdomainsAsAccountKeys\">http:\/\/wiki.rubyonrails.org\/rails\/pages\/HowToUseSubdomainsAsAccountKeys<\/a><\/p>\n<p>After setting up the plugin, I&#8217;m able to access the subdomain name anywhere in the app. Good thing, because I need to access it in application.rhtml (or .haml)<\/p>\n<pre lang=\"rails\">\n\n<% # if there's a subdomain, render the partial of the same name -%>\n<% if account_subdomain -%>\n    <% # first check to make sure account_subdomain is valid and in accounts table  -%>\n    <% @account = Account.find_by_username(account_subdomain) -%>\n    <% unless @account.nil? -%>\n        <%= render :partial => \"layouts\/\"+account_subdomain %>\n    <% else -%>\n        <% # There's a subdomain, but it isn't valid. Render default template -%>\n        <%= render :partial => \"layouts\/default\" %>\n    <% end -%>\n<% else -%>\n    <%= render :partial => \"layouts\/default\" %>\n<% end -%>\n<\/pre>\n<p>Notice in the above, that there are no :yield statements. That&#8217;s because :yield can live in a partial too. I&#8217;ve set up a partial for each subdomain listed in my database in app\/views\/layouts, and added some logic to render the default layout when a subdomain either isn&#8217;t present or is invalid.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say I want to deploy my app as a service to several good customers, but I want my app to share the exact layout as each customer site, so it appears to be an internal app to their company and its own clients. I take thier CSS and HTML markup, and make it into a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,15],"tags":[45,46],"_links":{"self":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/5"}],"collection":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5"}],"version-history":[{"count":0,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions"}],"wp:attachment":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}