Home  >  Q&A  >  body text

ruby - rails路由constraints问题

初学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
黄舟黄舟2758 days ago655

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师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

    reply
    0
  • PHPz

    PHPz2017-04-25 09:04:04

    var code=me

    reply
    0
  • Cancelreply