初学rails,有几个问题不明白。
class NamespaceConstraint
def self.matches?(request)
name = request.fullpath.split('/').second.downcase
if name[0] == '~' then name = name[1..-1] end
ns = Namespace.where(name_lower: request.fullpath.split('/').second.downcase).first
not ns.nil?
end
end
Rails.application.routes.draw do
constraints(NamespaceConstraint) do
get ':namespace' => 'namespaces#show'
end
end
这段代码大致是什么意思?
self.matches?
这个函数名问号是什么意思?
这个request
变量名并没有定义,是rails自动生成的吗?
not ns.nil?
这个是啥意思?
天蓬老师2017-04-25 09:04:04
In ruby, the return value of methods with ? is agreed to be true/false
request is a custom variable in rails controller, and the corresponding response is also
not means negation, for example, not true means false, ns.nil? What is returned is a boolean type, not negated
This method is used to match routes. request.fullpath returns a relative path. For example, blogs.com/blogs returns /blogs. Then the first line finally obtains 'blogs', and then uses 'blogs' to find the current path. Whether the route matches it, true if yes, false if not.
You can try all of these methods locally and run them step by step. This is not a complicated thing