Rails route to model instance - by domain name -
i have rails 3 application, say, hotels, hotels belong parent areas. when user hits app (served mongrel >> nginx), want domain name used in request decide area of hotels serve (domain name >> area). achieve can see 2 options:
1) rewrite url nginx, inserting area id after domain name (e.g. birminghamhotels.co.uk => proxy_pass http://myupstream/areas/3$request_uri).
benefits: domain object mapping happens accepted domains defined: nginx.conf. should transparent users (pretty urls possible rewritten).
drawbacks: breaks rails url helpers, no more link_to or form_for. hard-coded object reference naughty.
2) catch domain name in routes.rb , area via unique "domain" attribute each area (or has_many if fancy).
benefits: should allow use of rails url helpers. requested domain linked directly resource exceptions can handled.
drawbacks: without rewriting urls nginx, wouldn't users see: birminghamhotels.co.uk/areas/3/hotels/42 instead of birminghamhotels.co.uk/hotels/42? also, don't have clue how it!
so, i've tried option #1 ran trouble url helpers etc. i've tried come way of trying option #2 haven't been able suss correct syntax despite lot of ogling. fact cannot find answers searching makes me think got wrong end of problem. there third option? how solve it?
oh , btw, not building hotel listing website - plenty enough of around already. happened close enough example.
assuming each area has sort of domain_name field, seems should able this:
class hotelscontroller < applicationcontroller def index @hotels = area.find_by_domain_name(request.subdomains.first).hotels end end
you refactor named_scope using lambda if like.
Comments
Post a Comment