Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 21st, 2011, 09:07 PM
CLEM_C_ROCK CLEM_C_ROCK is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 174 CLEM_C_ROCK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 27 m 11 sec
Reputation Power: 11
Rails 3 Foreign Domain routing - cannot get this to work!

Hello,
I'm trying to build an app (mysuperapp.com) that allows users to create an account in which a default subdomain (IE: joeshmoe.mysuperapp.com) will be
created as their homepage. In their profile settings, I also want to give them the ability to add their own domain name so their homepage now can be joeshmoe.com. I know this is possible in rails w/ plugins like subdomainfu and w/out plugins in rails3. I've searched countless websites on the subject and I'm attempting to build an app based on this example:

http://glacialis.postmodo.com/

so, as in this example, I've created lib/personalized_domain.rb

Code:
# lib/personalized_domain.rb
class PersonalizedDomain
  def self.matches?(request)
    case request.host
    when 'www.#{APP_CONFIG[:domain]}', '#{APP_CONFIG[:domain]}', nil
      false
    else
      true
    end
  end
end



and in my routes I have this:

Code:
# config/routes
constraints(PersonalizedDomain) do
  namespace :personalized, :path => '/' do
    root :to => "projects#show"
    resource :project
  end
end


root :to => "home#index"



in my model I have:

Code:
# app/models/project.rb
before_validation :cache_domain

validates_uniqueness_of :subdomain, 
                        :cached_domain
validates_uniqueness_of :cname_alias, 
                        :allow_blank => true
validates_presence_of   :subdomain,
                        :cached_domain
validates_exclusion_of  :subdomain,
                        :in => %w(admin blog), 
                        :message => "is taken"

def cache_domain
  self.cached_domain = if self.cname_alias.blank?
    "#{self.subdomain}.#{APP_CONFIG[:domain]}"
  else
    "#{self.cname_alias}"
  end
end



as per the example, my base controller:
Code:
# app/controllers/personalized/base_controller.rb
class Personalized::BaseController < ApplicationController
  before_filter :get_project

protected
  def get_project
    @project = Project.first(:conditions => { 
        :cached_domain => request.host 
    })
  end
end



One thing I noticed, in PersonalizedDomain, was that if I put a debug statement in the self.matches? method as such:

Code:
class PersonalizedDomain
  def self.matches?(request)
    Rails.logger.info("****(PersonalizedDomain::self.matches?)-->>APP_CONFIG[:domain]: #{APP_CONFIG[:domain]}")
    case request.host
    when 'www.#{APP_CONFIG[:domain]}', '#{APP_CONFIG[:domain]}', nil
      false
    else
      true
    end
  end
end


the debug statement never get's executed.

Thanks for any help

Reply With Quote
  #2  
Old April 29th, 2011, 09:48 AM
CLEM_C_ROCK CLEM_C_ROCK is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 174 CLEM_C_ROCK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 27 m 11 sec
Reputation Power: 11
Here's an update - I did figure out why my custom constraint wasn't working correctly and here's the changes I made:

In PersonalizedDomain I changed the self.matches? method from a class method to an instance method as such:

Code:
# lib/personalized_domain.rb
class PersonalizedDomain
  def matches?(request)
    case request.host
    when "www.#{APP_CONFIG[:domain]}", "#{APP_CONFIG[:domain]}", nil
      false
    else
      true
    end
  end
end


and in routes I did this:

Code:
constraints(PersonalizedDomain.new) do
      resources :posts
      resources :personalized
  end

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Rails 3 Foreign Domain routing - cannot get this to work!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap