aceofbassgreg
aceofbassgreg

Reputation: 3947

Dynamic Routing not rendering

So, I have a named route:

match 'ip/get/:ip' => 'ip_addresses#show', :via => :get

As you can see, I'd like the ip (after 'get') to be dynamic, but I keep getting a routing error when I try it out. Here are my routes:

  root        /                     ip_addresses#index
ip_add POST   /ip/add(.:format)     ip_addresses#create
       GET    /ip/add(.:format)     ip_addresses#new
ip_all GET    /ip/all(.:format)     ip_addresses#index
       GET    /ip/get/:ip(.:format) ip_addresses#show
       DELETE /ip/all(.:format)     ip_addresses#destroy

And here's my show action:

def show
  IpAddress.find(params[:id])
end

EDIT: Routing error:

ActionController::RoutingError (No route matches [GET] "/ip/get/1.2.3.4"):

I've read the Rails Routing from the Outside In Guide (http://guides.rubyonrails.org/routing.html) but naturally I may be overlooking something. Any help is appreciated. Thanks!

Upvotes: 0

Views: 76

Answers (1)

Aleks
Aleks

Reputation: 5388

The answer to your question lays in article you gave.

Take a look at section:

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 which overrides this – for example :id => /[^/]+/ allows anything except a slash.

Look at the example there:

match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/

So in your example I believe it would be:

match 'ip/get/:ip' => 'ip_addresses#show', :id => /[^/]+/ , :via => :get

And also change params[:id] to params[:ip]

Upvotes: 2

Related Questions