Rails路由到模型实例-按域名 [英] Rails route to model instance - by domain name

查看:81
本文介绍了Rails路由到模型实例-按域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在旅馆中有一个Rails 3应用程序,其中旅馆属于父区域.当用户点击该应用程序(由mongrel >> nginx服务)时,我希望请求中使用的域名来决定要服务的酒店区域(域名>>区域).为此,我可以看到两个选项:

I have a Rails 3 application, say, with hotels, where hotels belong to parent areas. When a user hits the app (served by mongrel >> nginx), I want the domain name used in the request to decide what area of hotels to serve up (domain name >> area). To achieve this I can see two options:

1)用nginx重写URL,在域名后插入区域ID(例如birminghamhotels.co.uk => proxy_pass http://myupstream/areas/3 $ request_uri).

1) Rewrite the URL with nginx, inserting the area id after the domain name (e.g. birminghamhotels.co.uk => proxy_pass http://myupstream/areas/3$request_uri).

好处:域到对象的映射发生在定义了可接受域的位置:nginx.conf.对用户应该透明(重写后可能会有漂亮的URL).

Benefits: Domain to object mapping happens where accepted domains are defined: nginx.conf. Should be transparent to users (pretty URLs possible as they are rewritten).

缺点:中断Rails url帮助器,不再需要link_to或form_for.硬编码对象引用很顽皮.

Drawbacks: Breaks Rails url helpers, no more link_to or form_for. Hard-coded object reference is naughty.

2)在route.rb中捕获域名,并通过每个Area的唯一"domain"属性(甚至是has_many,如果您愿意的话)通过唯一的"domain"属性查找Area.

2) Catch the domain name in routes.rb and look up the Area via a unique "domain" attribute for each Area (or even a has_many if you fancy).

优点:应该允许使用所有的Rails URL帮助器.请求的域直接链接到资源,因此可以处理异常.

Benefits: Should allow use of all Rails URL helpers. Requested domain linked directly to resource so exceptions can be handled.

缺点:如果不使用nginx重写URL,用户不会看到:birminghamhotels.co.uk/areas/3/hotels/42而不是birminghamhotels.co.uk/hotels/42吗?另外,我不知道该怎么做!

Drawbacks: Without rewriting URLs with nginx, wouldn't users see: birminghamhotels.co.uk/areas/3/hotels/42 instead of just birminghamhotels.co.uk/hotels/42? Also, I don't have a clue how to do it!

因此,我尝试了选项#1,但是遇到了URL辅助程序等问题.我试图提出一种尝试选项#2的方法,但是尽管有很多,但仍无法使用正确的语法的.现在,我无法通过搜索找到任何答案,这一事实使我觉得我对这个问题有错误的认识.还有第三种选择吗?您将如何解决?

So, I've tried option #1 but ran into trouble with URL helpers etc. I've tried to come up with a way of trying option #2 but haven't been able to suss the correct syntax despite a lot of ogling. Now the fact I cannot find any answers by searching makes me think I got the wrong end of this problem. Is there a third option? How would you solve it?

哦,顺便说一句,我没有建立另一个酒店列表网站-已经有足够的网站了.刚刚碰巧是一个足够接近的例子.

Oh and btw, I am not building another hotel listing website - plenty enough of those around already. Just happened to be a close enough example.

推荐答案

假设每个区域都有某种domain_name字段,看来您应该能够执行以下操作:

Assuming each area has some sort of domain_name field, it seems like you should be able to do something like this:

class HotelsController < ApplicationController
  def index
    @hotels = Area.find_by_domain_name(request.subdomains.first).hotels
  end
end

如果愿意,您甚至可以使用lambda将其重构为named_scope.

You could even refactor it into a named_scope using a lambda if you like.

这篇关于Rails路由到模型实例-按域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆