class ApplicationController < ActionController::Base
protect_from_forgery #What is this syntax? When is this executed and how to create one?
end
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :body, :commenter, :post
end
在第一种情况下,我了解ApplicationController是模块ActionController中新的Derived类,名为Base
。下一行会发生什么? protect_from_forgery
是基类还是模块ActionController中的方法?这叫什么?我无法在Ruby类文档中找到。我尝试在基类中创建一个方法,但得到如下错误。我如何创建可以在课堂上使用的特殊命令?ruby clases里面的语法是什么?
class Base
def foo
@name = "foo"
end
end
class Der < Base
foo
def bar
@dummy = "bar"
end
end
错误:
expr1.rb:62:in `<class:Der>': undefined local variable or method `foo' for Der:Class (NameError)
from expr1.rb:61:in `<main>'
那些是rails方法,而不是ruby。这就是为什么你找不到它们。 [forgery_protection](http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html)。这是[ActionController]中的类方法(https://github.com/rails/rails/blob/e7feaff70f13b56a0507e9f4dfaf3ebc361cb8e6/actionpack/lib/action_controller/metal/request_forgery_protection.rb#L102) –
@japed如何创建此类方法?何时执行?为什么我的例子不起作用? – balki