partials是Rails模板重用的一项重要技术,让我们来读读partials.rb源码文件:
module ActionView
module Partials
def render_partial(partial_path, local_assigns = nil, deprecated_local_assigns = nil)
path, partial_name = partial_pieces(partial_path)
object = extracting_object(partial_name, local_assigns, ...
- 15:37
- 浏览 (1423)
- 论坛浏览 (1660)
- 评论 (0)
- 分类: Ruby
先看源码再分析
1,action_view.rb
$:.unshift(File.dirname(__FILE__) + "/action_view/vendor")
require 'action_view/base'
require 'action_view/partials'
ActionView::Base.class_eval do
include ActionView::Partials
end
ActionView::Base.load_helpers(File.dirname(__FILE__) + "/action_view/helpers/")
...
- 14:53
- 浏览 (1477)
- 论坛浏览 (1865)
- 评论 (0)
- 分类: Ruby
1,action_controller\pagination.rb:
module ActionController
module Pagination
def paginate(collection_id, options={})
Pagination.validate_options!(collection_id, options, true)
paginator_and_collection_for(collection_id, options)
end
def self.validate_options!(co ...
- 17:41
- 浏览 (2299)
- 论坛浏览 (2851)
- 评论 (7)
- 分类: Ruby
Rails从HTTP Accept header得到客户端需要的response format信息
默认的MIME types见mime_type.rb:
ALL = Type.new "*/*", :all
TEXT = Type.new "text/plain", :text
HTML = Type.new "text/html", :html, %w( application/xhtml+xml )
JS = Type.new "text/javascript", :js, %w( application/javascript applicatio ...
- 11:14
- 浏览 (1488)
- 论坛浏览 (1677)
- 评论 (3)
- 分类: Ruby
深入了解一下ActionController的Resources--RESTful Rails
1,ActionController的resources用来实现REST api,一个单独的resource基于HTTP verb(method)有不同的行为(action),如:
map.resources :messages
class MessagesController < ActionController::Base
# GET messages_url
def index
# return all messages
end
# GE ...
- 01:58
- 浏览 (2440)
- 论坛浏览 (2552)
- 评论 (4)
- 分类: Ruby
我们上次看过了ActiveRecord的callbacks,这次看看ActionController的filters
1,filter继承
先执行父类中的filter,再执行子类中的filter,如果父类中的filter返回false,则不执行子类中后续的filter
2,filter类型
1)method reference(symbol)
class BankController < ActionController::Base
before_filter :audit
end
2)external class
class OutputCompressionFi ...
- 17:44
- 浏览 (1484)
- 论坛浏览 (1545)
- 评论 (0)
- 分类: Ruby
Pablo Hoch给notepad2加上了Ruby/YAML语法高亮,我们可以把它当作windows下的轻量级Ruby IDE来使了
特点:
green, fast, free
支持的schemes:
HTML/XML//CSS/JavaScript/VBScript/Perl/CGI Script/ActionScript 2.0/C/C++/C#/Resource Script/Makefiles/Java/Visual Basic/Pascal/Assembler/SQL/Python/NSIS Script/Configuration Files/Batch Files/Diff ...
- 15:45
- 浏览 (3257)
- 论坛浏览 (3659)
- 评论 (8)
- 分类: Ruby
2007-06-26
ActiveRecord的lazy loading与eager loading
关键字: ActiveRecord lazy loading eager loading
看来大家还对ActiveRecord的lazy loading和eager loading不是很清楚
ActiveRecord默认是lazy loading的,而加上:include选项后可以指定eager loading一些字段
:include - specify second-order associations that should be eager loaded when the collection is loaded.
看例子:
Post.find(:all, :include => :comments)
# => SELECT ... FROM po ...
- 14:10
- 浏览 (1103)
- 论坛浏览 (1222)
- 评论 (0)
- 分类: Ruby
看看Rails的request/response源码吧,非常有趣,有些方法非常实用
1,request.rb:
module ActionController
class AbstractRequest
def method
@request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ?
parameters[:_method].to_s.downcase.to_sym :
@env['REQUEST_MET ...
- 13:04
- 浏览 (1572)
- 论坛浏览 (1970)
- 评论 (0)
- 分类: Ruby
cookies.rb:
module ActionController
module Cookies
protected
def cookies
CookieJar.new(self)
end
end
class CookieJar < Hash
def initialize(controller)
@controller, @cookies = controller, controller.request.cookies
super()
update(@ ...
- 19:20
- 浏览 (1336)
- 论坛浏览 (1496)
- 评论 (0)
- 分类: Ruby
我们知道Rails默认使用file来存储session数据,放在tmp\sessions目录下
其实我们还可以使用数据库、drb_server、mem_cache甚至内存来存储session数据
方法就是更改environment.rb:
config.action_controller.session_store = :active_record_store || :drb_store || :mem_cache_store || :memory_store
当然使用数据库存储session数据时要先创建数据库表
rake db:sessions:create
这在以前的Ra ...
- 18:42
- 浏览 (1241)
- 论坛浏览 (1592)
- 评论 (0)
- 分类: Ruby
Rails里富有Magic的东西往往实现起来非常简单,比如scaffolding
看看代码先,scaffolding.rb:
module ActionController
module Scaffolding
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def scaffold(model_id, options = {})
options.assert_valid_keys(:class ...
- 17:30
- 浏览 (1322)
- 论坛浏览 (1980)
- 评论 (3)
- 分类: Ruby
满城尽带黄金甲,源码尽在routing.rb:
module ActionController
module Routing
class Route
attr_accessor :segments, :requirements, :conditions
end
class RouteBuilder
def build(path, options)
path = "/#{path}" unless path[0] == ?/
path = "#{path}/" unless path[ ...
- 22:31
- 浏览 (1363)
- 论坛浏览 (1605)
- 评论 (0)
- 分类: Ruby
2007-06-24
Rails源码研究之ActionController:一,基本架构、render、redirect
关键字: ActionController render redirect 源码
1,action_controller.rb:
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
unless defined?(ActiveSupport)
begin
$:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib")
require 'active_sup ...
- 01:06
- 浏览 (2427)
- 论坛浏览 (2877)
- 评论 (1)
- 分类: Ruby
ActiveRecord自带了三种数据结构关系:acts_as_tree、acts_as_list、acts_as_nested_set
1,tree.rb
module ActiveRecord
module Acts
module Tree
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_tree(options = {})
configu ...
- 19:47
- 浏览 (1853)
- 论坛浏览 (2255)
- 评论 (4)
- 分类: Ruby
Callbacks相关的源码在callbacks.rb文件里:
module ActiveRecord
module Callbacks
CALLBACKS = %w(
after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation
after_validation before_validation_on_create after_validation_on_cre ...
- 11:12
- 浏览 (1295)
- 论坛浏览 (1469)
- 评论 (0)
- 分类: Ruby
Validations相关的源码全在validations.rb文件里:
module ActiveRecord
class Errors
include Enumerable
@@default_error_messages = {
:inclusion => "is not included in the list",
:exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confi ...
- 20:09
- 浏览 (1771)
- 论坛浏览 (2111)
- 评论 (4)
- 分类: Ruby
这次我们分析一下Rails的事务支持
1,Rails默认将父子关系的表的save()和destroy()包装在一个事务里(见AWDWR一书的Transactions)
这保证了父子保存和删除的原子性,即ActiveRecord是级联保存和级联删除的,有源码为证
transactions.rb:
module ActiveRecord
module Transactions
def self.included(base)
base.extend(ClassMethods)
base.class_eval do
[:destroy, ...
- 12:59
- 浏览 (1423)
- 论坛浏览 (1474)
- 评论 (0)
- 分类: Ruby
今天学习一下ActiveRecord的Associations相关的源码,即了解一下我们常用的has_many、has_one、belongs_to、has_and_belongs_to_many的原理
1,activerecord-1.15.3\lib\active_record\associations.rb:
require 'active_record/associations/association_proxy'
require 'active_record/associations/association_collection'
require 'active_recor ...
- 22:06
- 浏览 (1977)
- 论坛浏览 (2251)
- 评论 (2)
- 分类: Ruby
Rails的ORM框架ActiveRecord是马大叔的ActiveRecord模式的实现+associations+SingleTableInheritance
ActiveRecord的作者也是Rails的作者--David Heinemeier Hansson
ActiveRecord的key features:
1,零Meta Data,不需要XML配置文件
2,Database Support,现在支持mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase,写一个新的databas ...
- 21:01
- 浏览 (3205)
- 论坛浏览 (3324)
- 评论 (1)
- 分类: Ruby
顺序看了《Agile Web Development With Rails》、《Rails Recipes》和《Ruby for Rails》,我看的都是最新的英文版的,说说对这三本书的感受。
1,《Agile Web Development With Rails》,2ed
以实践为基础,一步步讲解Web程序开发Rails做法,非常简单易懂,对没有编程背景的人来看也不是什么难事,算是本不错的Rails入门书。
但既然作为入门书,只看了本书的朋友们就不要在简历中写什么“熟悉Ruby on Rails”了。
2,《Rails Recipes》
这本书是Rails开发的参考书,一篇一篇讲解Rai ...
- 16:47
- 浏览 (16222)
- 论坛浏览 (17191)
- 评论 (38)
- 分类: Ruby
1, 用"class <<"定义class method
class User < ActiveRecord::Base
class << self
def authenticate(username, password)
find_by_username(username, :conditions => [password_hash = ?", Digest::SHA1.hexdigest(passwword)])
end
2, 用self.included(class)方法作为include模块的hooks
modul ...
%Y
Year(four digits)
%y
Year(last two digits)
%b, %B
Short month name, full month name
%m
Month(number)
%d
Day of month(left-padded with zeros)
%e
Day of month(left-padded with blanks)
%a, %A
Short day name, full day name
%H, %I
Hour(24-hour clock), hour(12-hour clock)
%M
Minute
%S
Second
...
2007-06-14
Ruby methoswith operator-style syntactic sugar calling notat
关键字: operator-style syntactic sugar calling notation
1, +
Definition:
def +(x)
Calling:
obj.+(x)
Sugared notation:
obj + x
2, -
Definition:
def -(x)
Calling:
obj.-(x)
Sugared notation:
obj - x
3, *
Definition:
def *(x)
Calling:
obj.*(x)
Sugared notation:
obj * x
4, /
Definition:
def /(x)
Calling:
obj./(x)
...
1, String
Quotation marks
"new string" or 'new string'
2, Symbol
Leading colon
:symbol or :"symbol with spaces"
3, Array
Square brackets
[1, 2, 3, 4, 5]
4, Hash
Curly braces
{"New York" => "NY", "Oregon" => "OR"}
5, Range
Two or three dots
0...10 or 0..9
6, Regexp
Forward sl ...
1, RuntimeError
Reason: This is the default exception raised by the raise method
raise
2, NoMethodError
Reason: An object is sent a message it can't resolve to a method name
a = Object.new
a.some_unkonw_method_name
3, NameError
Reason: The interpreter hits an identifier it can't resolve a ...
Ruby里方法的查找顺序是本类=>本类include的模块=>父类=>父类include的模块...
而当最终还是没有找到该方法时,会触发内建的method_missing方法的执行
默认的method_missing会触发异常,而我们可以通过override method_missing方法来提供一些特性
比如Rails的ActiveRecord,User.find_by_name()这个方法本来没有在User类里定义,但是可以通过override method_missing来匹配数据库fields
然后使用class_eval和send动态创建和调用查询方法
我们来看看C:\ruby\ ...
虽然N年以前就知道有CDT这个东西
但是知道今晚试过CDT才知道安装Visual C++是个sb似的行为
感觉删除你电脑上的VC程序,下载以下三样东西:
1,MinGW
2,Eclipse
3,CDT
这三样东西最大的相同点就是开源免费
MinGW下载MinGW-3.1.0-1.exe文件然后安装即可
Eclipse、CDT的安装就不需多费口舌了
如果CDT安装后没效果,删除D:\eclipse\configuration\org.eclipse.osgi目录下的manifest文件夹、.bundledata和.state文件然后重启Eclipse即可
然后在系统环境变量path中加入Mi ...
- 02:19
- 浏览 (2620)
- 评论 (0)
Ferret是Ruby的文本搜索引擎,它基于Apache Lucene
安装Ferret非常简单:
gem install ferret
Ferret是一堆C代码的Ruby代码封装,Ferret是针对Ruby的而不是RoR的
而Acts As Ferret则是针对RoR的
我们有两种方式安装Acts As Ferret:
1,以gem方式安装
gem install acts_as_ferret
然后在environment.rb里添加
require 'acts_as_ferret'
2,以plugin方式安装
ruby script/plugin inst ...
今天来看看使用upload_progress插件监听文件上传status以及创建AJAX上传progress bar
Requirements:http://sean.treadway.info/articles/2005/07/18/upload-progress-checklist
安装好该插件以及Requirements后,我们就可以在controller里使用了:
class DocumentController < ApplicationController
upload_status_for :create
def create
# ... Y ...
- 浏览: 723203 次
- 性别:

- 来自: BJ

- 详细资料
搜索本博客
我的相册
screenshot
共 1 张
共 1 张
最近加入圈子
最新评论
-
Why OO sucks
看成去QQ SUCK 了
-- by xhanxhanxhan -
Rails的富文本编辑器插件 ...
有没有那个像textmate那种语法高亮的富文本编辑器?
-- by zllicho -
翻译www.djangobook.com之 ...
weiertzw 写道 1. >>> from django. ...
-- by chenjihua75 -
PHP、CakePHP哪凉快哪呆 ...
这孩子被java毒害太深。。。跳出java,你会发现外面的世界真的很大。
-- by woodless -
学习svn命令
只会用 apt-get 不是好孩子。
-- by smartly






评论排行榜