Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Routes - custom subdomains and adding admin namespace

sudomains

  • Please log in to reply
2 replies to this topic

#1 bigLund8

bigLund8

    Passenger

  • Members
  • 2 posts

Posted 28 April 2014 - 10:57 PM

I set up my rails app to use custom subdomains following this awesome RailsCast tutorial by Ryan Bates. Users can visit company.lvh.me:3000 and view all its relevant information. Now, I would like to add an admin subdomain on the front of the custom subdomain (admin.company.lvh.me:3000).
 
The idea is that I have specific admins for each blog with special controllers/views. So if admin is added to the front of the company subdomain, rails will route through the app/controllers/admin/blogs_controller.rb and show the app/views/admin/blogs/show.html.erb.
 
I've used the admin namespace and set it as a subdomain in some of my past Rails apps, but when I try that here I need the second subdomain to be dynamic:
namespace :admin, path: '/', constraints: { subdomain: 'admin.DYNAMIC' } do
  match     '/',            to: 'blogs#show', via: 'get'
end

So if type admin.company in the subdomain constraint, it works like a charm, but how can make it dynamic? I've had no luck making it dynamic ('admin.' + Subdomain, etc.) in the routes files, which lead me to the thought: can I just use Ryan Bates's Subdomain class?

namespace :admin, path: '/', constraints: { subdomain: Subdomain } do
  match '/', to: 'blogs#show', via: 'get'
end

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end 
end

Relevant Routes
Prefix Verb  URI Pattern  Controller#action
 admin GET   /            admin/blogs#show
The routes even seem right, but when I try this, it tries to route through the normal blogs_controller.rb!
 
Am I'm close? Is there a better way?


#2 Ohm

Ohm

    Driver

  • Moderators
  • 479 posts
  • LocationCopenhagen

Posted 30 April 2014 - 04:55 AM

'blogs#show' will route to your BlogsController. Are you sure it shouldn't be something like 'admin_blogs#show'?

 

Also, the 'show' action is often used together with an id, while the 'index' action is listing all the entries, and are thus id-less.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 bigLund8

bigLund8

    Passenger

  • Members
  • 2 posts

Posted 30 April 2014 - 05:25 AM

I'm actually using the 'show' action in this situation as the 'home' page for the blog and I set the blog according to the subdomain (not an id passed as a parameter). It feels a little hackish, but I was able to get it working:

routes.rb

constraints(Subdomain) do
  namespace :admin, module: "admin", path: "", constraints: lamda { |r| r.subdomain.split('.')[0] == 'admin' } do
    match '/', to: 'blogs#show', via: 'get'
    ...
  end

  match '/', to: 'blogs#show', via: 'get'
  ...
end

Rake Routes:

  Prefix Verb URI Pattern Controller#Action
   admin GET  /   admin/blogs#show
   ...
   ‚Äč      GET / blogs#show
   ...

If anyone is looking to do something crazy like me, hopefully this helps! 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users