Last Updated: February 25, 2016
·
5.245K
· mrfoto

Rails routing and namespaced models

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.

1 Response
Add your response

I fought hard to get rid of namespace prefixes in generated urls from namespaced models, this 'rails magic' gave me only headaches and routes duplications to maintain models correctly. I even thought about using isolated namespaces somehow. This solution resolves all my problems. Dunno why it's not documented. Thanks for sharing!

over 1 year ago ·