帐户作为Rails中的顶级目录

时间:2009-09-25 02:00:49

标签: ruby-on-rails

网上有很多关于在Rails中处理子域帐户的文章。例如,请参阅this。但是,我找不到一种简单的方法来将帐户密钥作为网址的顶级目录。也就是说,我想转换我的

http://account.domain.tld网址

http://domain.tld/account

到目前为止,我迄今为止处理此问题的最佳全能宝石是routing filter

还有其他想法吗?

3 个答案:

答案 0 :(得分:2)

我想你可能想看看Subdomain_fu

答案 1 :(得分:0)

您需要设置一个wildcard component的自定义路线。将此类内容添加到 config / routes.rb

map.account ':/account', :controller => 'accounts', :action => 'show'

然后在AccountsController

def AccountsController < ApplicationController
  def show

    # Probably move this into a before_filter
    @account = Account.find_by_name(params[:account])
  end
end

答案 2 :(得分:0)

我会在控制器中执行此操作,或者甚至在服务器vhosts(Apache或Nginx或其他)上执行此操作。这是一个使用控制器的例子。

# App controller
class ApplicationController < ActionController::Base
  before_filter :monitor_subdomains

  private

  def monitor_subdomains
    return if !request.subdomains.empty? && !request.subdomains.first != "www"

    account = request.subdomains.first
    redirect_to "#{account}/#{request.request_uri}"
  end
end

# routes.rb
map.account ":account_name" do |account|
  account.resources :projects
  # more account related routes...
end

我不确定我的routes.rb示例是否有效,但是你得到了图片。