ruby - rails路由constraints问题
黄舟
黄舟 2017-04-25 09:02:04
0
2
690

初学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?这个是啥意思?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
Peter_Zhu
  • 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

PHPzhong
var code=me
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template