As a beginner of rails, I don’t understand a few questions.
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
-
What does this code roughly mean?
-
self.matches?
What does this function name question mark mean? -
This ..
request
The variable name is not defined, is it automatically generated by rails? -
not ns.nil?
What does this mean?
Ruby’s Convention Band? Is true/false
Request is a custom variable in rails controller, and so is the corresponding response.
Not is the opposite meaning, for example, not true is false. Returns a boolean, not inverted
This method is used to match routes. request.fullpath returns relative paths such as blogs.com/blogs and /blogs, then the first line finally obtains’ blogs’, and then’ blogs’ is used to find out whether the current route matches it, if yes, true and false.
All these methods can be tried and run step by step locally. This is not a complicated thing.