If you’re like me, you like code neatly organized in folders. With Rails we can simply do that with module namespaces. So lets say we have generated a scaffold Projects::Document . Now when using url_for or form_for or any other path |
url helper Rails would return the namespace in the route name like so: projects_documents_path . |
Sometimes you don’t want that, especially if you nest those resources. I for example have it nested under Project so Rails wants to find project_projects_documents_path
. That’s just too confusing and serves no real purpose.
I found a hard and hackish way to get rid of that using the self.model_name
method in the model and have it return ActiveModel::Name.new(self, nil, "Document")
but I didn’t like the solution because it really looks hackish and I had to do it in all the namespaed models.
But fortunately there is an easy way to solve that. It’s not documented anywhere and I don’t know why but it sits nice in the source. All you have to do is define a self.use_relative_model_naming?
in your module and have it return true
like so:
module Projects
def self.use_relative_model_naming?
true
end
end
Now Rails searches for project_documents_path
and all izz well.