`
hideto
  • 浏览: 2666867 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之account_location

    博客分类:
  • Ruby
阅读更多
《Rails Recipes》已经告一段落,今天开始一起学习Rails插件
首先安装我们今天要看的account_location插件:
ruby script/plugin install account_location

account_location很简单,就是几个helper方法,我们先看看源码:
# Copyright (c) 2005 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module AccountLocation
  def self.included(controller)
    controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url)
  end

  protected
    def default_account_subdomain
      @account.username if @account && @account.respond_to?(:username)
    end

    def account_url(account_subdomain = default_account_subdomain, use_ssl = request.ssl?)
      (use_ssl ? "https://" : "http://") + account_host(account_subdomain)
    end

    def account_host(account_subdomain = default_account_subdomain)
      account_host = ""
      account_host << account_subdomain + "."
      account_host << account_domain
    end

    def account_domain
      account_domain = ""
      account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
      account_domain << request.domain + request.port_string
    end

    def account_subdomain
      request.subdomains.first
    end
end

大家是否联想到蛙眼的用户子域名是怎么实现的?对,就是account_location插件
我们这样使用该插件:
class ApplicationController < ActionController::Base
  include AccountLocation
  before_filter :find_account
    
  protected
    def find_account
      @account = Account.find_by_username(account_subdomain)
    end
end

class BlogController < ApplicationController
  def index
    @topics = Topic.find_by_user_id(@account.id)
  end

  # ...
end

在view中:
Your domain: <%= account_url %>

不过这对于@account为nil时就会报异常,DHH写的代码也垃圾啊
所以我改写了一下account_location:
module AccountLocation
  def self.included(controller)
    controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url)
  end

  protected
  
    def account_url(use_ssl = request.ssl?)
      (use_ssl ? "https://" : "http://") + account_host
    end

    def account_host()
      account_host = ""
      if account_subdomain == ""
      	account_host << ""
      else
        account_host << account_subdomain + "."
      end
      account_host << account_domain
    end

    def account_domain
      account_domain = ""
      account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
      account_domain << request.domain + request.port_string
    end
    
    def account_subdomain
      request.subdomains.first || ""
    end
end

然后我们可以修改C:\WINDOWS\system32\drivers\etc\hosts或/etc/hosts,添加一行
127.0.0.1       blog.hideto.com

或者类似的本机域名来进行测试
分享到:
评论
5 楼 hideto 2008-08-04  
to qichunren:
使用lighttpd等web server配置一下:

参见 lighttpd配置子域名和重定向
4 楼 qichunren 2008-08-04  
那怎么样在用户注册时给他产生二级域名,然后并且也能能绑定到主站的IP地址呢?

另外,我看了这个,感觉这产生的二级域名是伪装的,是这样的吗???
3 楼 crazycode 2008-04-15  
关于multi site的测试,可参考:
http://railstips.org/2006/11/27/testing-a-multi-site-application
2 楼 hideto 2007-06-30  
不行,得看你启动的server的配置
1 楼 dongbin 2007-06-30  
测试比较麻烦,有没有可能在functional test 里面测试呢?

相关推荐

    rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板.zip

    rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板 Rails 应用编辑器 Rails 应用程序编辑器 gem 安装一个 命令行 工具来从"食谱"的Collection 组装 Rails 应用程序。"你可以使用 rails_...

    rails_admin_acts_as_list:rails_admin插件以对记录进行排序

    介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...

    rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器.zip

    rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器 Rails 语义记录器 语义记录器用语义记录器替代 Rails 缺省记录器。http://github.com/rocketjob/rails_semantic_logger文档有关完整文档...

    inspinia admin - v2.5 Rails_Full_Version

    "inspinia admin - v2.5 Rails_Full_Version" 是一个基于Rails框架构建的后台管理系统的完整版本。这个系统采用流行的Inspinia Admin模板,提供了丰富的功能和自定义选项,旨在帮助开发者快速构建高效、现代且用户...

    Api-rails5_api_tutorial.zip

    Api-rails5_api_tutorial.zip,了解如何在michael hartl的rails 5教程上构建一个现代api立即在rails应用程序中构建一个api!(Rails 5版本),一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web...

    InspiniaAdmin 2.6.1 Rails_Full_Version

    在Rails_Full_Version压缩包中,包含了完整的源代码和必要的资源文件,开发者可以通过解压并导入到Rails项目中,按照官方文档进行配置和定制。同时,这个版本可能还包含了升级记录、更改日志和可能的bug修复,以保证...

    rails open_flash_chart

    "Rails Open Flash Chart" 是一个基于Ruby on Rails框架的库,用于在Web应用程序中生成交互式的Flash图表。这个库允许开发者轻松地创建各种统计图表,如折线图、柱状图、饼图等,以便更好地可视化数据。由于Flash在...

    RestFul_Rails_Dev_pdf_v_0.1.zip

    本资料“RestFul_Rails_Dev_pdf_v_0.1.zip”包含了《RESTful Rails Development》的翻译版,将深入探讨如何在Rails中实现RESTful的设计模式。 首先,RESTful设计的核心概念是资源(Resources)。在Rails中,资源...

    agile_web_development_with_rails_3rd_edition.9994652073.pdf

    《敏捷Web开发与Rails》第三版是一本深入探讨Ruby on Rails框架在敏捷软件开发方法论下的应用指南。本书由多位知名作者共同编写,包括Sam Ruby、Dave Thomas、David Heinemeier Hansson等,他们都是在Ruby社区内享有...

    Rails_3_Cheat_Sheets.pdf

    Rails_3_Cheat_Sheets.pdf

    rails3 和 thinking_sphinx安装

    Thinking Sphinx 是一个为 Rails 应用程序提供全文搜索功能的插件,它将数据库中的数据与 Sphinx 搜索引擎紧密结合,提供高效且高度定制化的搜索体验。 在 Rails3 中集成 Thinking Sphinx,首先你需要确保你的系统...

    Ruby-on-Rails-rails.zip

    Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_...

    rails_best_practices:Rails项目的代码度量工具

    rails_best_practices rails_best_practices是用于检查Rails代码质量的代码度量工具。 它支持以下ORM / ODM: 活动记录 蒙古型 mongomapper 以及以下模板引擎: erb 哈姆 减肥 拉布尔 rails_best_practices...

    inspinia_admin_v2.5_Rails_Full_Version

    该模板包含的文件列表“inspinia_admin_v2.5_Rails_Full_Version”很可能包括以下组成部分: 1. **静态资源**:CSS样式文件、JavaScript脚本和图像资源。这些文件用于构建用户界面,包括响应式布局、图表、表单元素...

    Rails_Recipes_with_Source_Code

    《Rails Recipes with Source Code》是一本专注于Ruby on Rails框架实践技巧和源代码解析的书籍。Rails是基于Ruby语言的Web开发框架,以其“约定优于配置”(Convention over Configuration)的理念和“开发人员的...

    InspiniaAdmin 2.5 Rails_Seed_Project

    InspiniaAdmin 2.5 Rails_Seed_Project是一款基于Bootstrap框架的高级管理模板,专为Ruby on Rails开发者设计,旨在加速Web应用程序的开发过程。这个项目作为种子启动器,提供了完整的后台管理界面,包括丰富的UI...

    inspinia admin - v2.5 Rails_Seed_Project

    Rails_Seed_Project则是Inspinia Admin在Rails环境下的一个种子项目,它的主要目的是快速搭建一个具备基本功能的后台管理系统。通过这个项目,开发者可以快速启动一个新的Rails应用,并且拥有一个预配置的后台界面,...

    InspiniaAdmin 2.5 Rails_full_version

    InspiniaAdmin 2.5 Rails_full_version

Global site tag (gtag.js) - Google Analytics