【发布时间】:2016-01-21 11:09:13
【问题描述】:
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 中一个名为 Base 的新派生类。下一行会发生什么? protect_from_forgery 是基类中的方法还是模块 ActionController 中的方法?这叫什么?我在 ruby 类文档中找不到。我尝试在基类中创建一个方法,但出现如下错误。如何创建可以在类中使用的特殊命令?
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。是ActionController中的类方法@
-
@japed 我如何创建这样的方法?什么时候执行?为什么我的示例不起作用?
标签: ruby-on-rails ruby