1

In my config/routes.rb file, I created a nested resource as follows:

resources :tags, only: [] do
  resources :blogs, only: [:index]
end

The problem is, if I create a tag named node.js, when I access the page by:

http://0.0.0.0:3000/tags/node.js/blogs

I get a routing error:

No route matches [GET] "/tags/node.js/blogs"

How do I get routing to work properly for resources with a dot in the name?

Rob Wise
  • 4,930
  • 3
  • 26
  • 31
j-zhang
  • 693
  • 1
  • 8
  • 17

2 Answers2

2

From Rails Routing from the Outside In, section 3.2:

By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, id: /[^/]+/ allows anything except a slash.

Therefore, make your route like this:

resources :tags, only: [], id: /[^\/]+/ do
  resources :blogs, only: [:index], id: /[^\/]+/ 
end
Rob Wise
  • 4,930
  • 3
  • 26
  • 31
1

Take a look at this post https://stackoverflow.com/a/5369702

The dot (.) Is normally use to separate

That would for example be:

get "/:user/contributions" => 'users#contributions', :constraints => { :user => /[^\/]+/ }
Community
  • 1
  • 1
MZaragoza
  • 10,108
  • 9
  • 71
  • 116