如何強制Rails考慮像點google.com
(例如/some_action/google.com
)中的一個參數,而不是"id" => "google", "format"=> "com"
?Rails - 帶「dot」的參數(例如/google.com)
的參數值應爲"id" => "google.com"
如何強制Rails考慮像點google.com
(例如/some_action/google.com
)中的一個參數,而不是"id" => "google", "format"=> "com"
?Rails - 帶「dot」的參數(例如/google.com)
的參數值應爲"id" => "google.com"
默認情況下,動態段不接受點 - 這是因爲該點被用作格式化路線的分隔符。但是,您可以爲路由參數添加一些正則表達式要求。在這裏,你想讓參數中的點。
match 'some_action/:id' => 'controller#action', :constraints => { :id => /[0-z\.]+/ }
而在導軌2.3:
map.connect 'some_action/:id', :controller => 'controller', :action => 'action', :requirements => { :id => /[0-z\.]+/ }
在Rails 4 I中:
get 'operation/:p1/:p2', to: 'operation#get', constraints: { p1: /[^\/]+/, p2: /[^\/]+/ }
它允許在兩個PARAMS任何字符(除 '/' )
這也適用於非拉丁郵件! – kadrian 2014-12-05 07:46:27
和與資源符號使用時,可以這樣進行:
resources :post,
only: [ :create, :index, :destroy ],
constraints: { id: /[0-z\.]+/ }
測試用Rails 4.1
我們,當我們卸下了API路徑的某些部分也有類似的情況。基本上,我們從/api/app/v1/*
去/api/v1/*
我們把這個在我們的路線
match '/api/app/v1/*path', to: redirect(path: '/api/v1/%{path}'), via: :all
這是所有罰款除了一些路線與路徑結束PARAMS包括點。例如。 /api/v1/foo/00.00.100
其中.100
得到解析成format
,其餘PARAM只有價值00.00
我們與存在params一些約束守衛這一點。
put '/api/app/v1/foo/:version',
constraints: { version: /([0-9]+)\.([0-9]+)\.([0-9]+)/ },
to: redirect('/api/v1/foo/%{version}')
編輯:我們使用的軌道5
最後Rails3中工作的解決方案!謝謝。 – lzap 2011-05-12 11:37:03