2013-03-27 171 views
0

所以,我有一個名爲路線:動態路由不渲染

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

正如你所看到的,我想在IP(後「得到」)是動態的,但我不斷收到路由錯誤當我嘗試它。這裏是我的路線:

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 

這是我的show行動:

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

編輯:路由錯誤:

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

我從外面看Rails的路由在指南( http://guides.rubyonrails.org/routing.html)但自然我可能會忽略某些東西。任何幫助表示讚賞。謝謝!

+0

你能解釋一下你想完成什麼?看看代碼和路線,我懷疑這是解決這個問題的好辦法。雖然這不是你的答案的直接答案,但我可以建議更乾淨的代碼,這樣做是一樣的。 – Aleks 2013-03-27 13:04:54

+0

你能分享你收到的錯誤嗎? – KULKING 2013-03-27 13:04:56

+0

Aleks,我同意這可能會更清潔,但我基本上完成了一個編碼練習,它要求命名的路由是RESTful的,並且在#show的情況下是動態的。如果您對提供的代碼有任何建議,請隨時添加! – aceofbassgreg 2013-03-27 13:17:08

回答

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

看看那裏的例子:

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

所以,在你的榜樣,我相信這將是:

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

,改變params[:id]params[:ip]

+0

Oooooooooh,我早些時候讀過那部分,現在就讀了,想着「但是我沒有做,除非你將點數算作冒號「,然後它讓我知道IP地址有點。 D'哦。讓我試試你的建議! – aceofbassgreg 2013-03-27 13:28:49

+0

爲了澄清,我的路由動作應該如下所示:'match'ip/get /:ip =>/[^ /] + /'=>'ip_addresses#show',:via =>:get'?有時Rails指南對我來說有點簡潔。 – aceofbassgreg 2013-03-27 13:33:03

+0

你也有一個例子,在網頁上的'match'photos /:id'=>'photos#show',:id =>/[AZ] \ d {5} /',所以在你的情況下,我相信它會匹配ip/get /:ip'=>'ip_addresses#show',:id =>/[^ /] + /,:via =>:get' – Aleks 2013-03-27 15:05:30