2016-06-14 33 views
0

我有一個模型,如Trips(id:integer, from: string, to: string, date: date)Rails - 使用「namespaced」查詢db網址

如何設置我的路線和trips_controller爲了能夠查詢我的數據庫只是像root_path/trips/:from/:to/:date URL?

+0

,你能否告訴我們,網址的例子嗎? – Wikiti

+0

東西像root_path /旅行/波士頓/芝加哥/ 2016-07-01 – davideghz

回答

1

嘗試添加該到你的路由文件config/routes.rb

get 'trips/:from/:to/:date', action: 'index', controller: 'trips' 

而且你的控制器看起來應該是這樣:

class TripsController < ApplicationController 
    # You can use whatever action instead of 'index' 
    def index 
    from = params[:from] 
    to = params[:to] 
    date = params[:date] # You can even parse this with Date.parse(params[:date]) 

    # Do whatever you want with those values 
    end 
end 
+0

真棒,它的作品:)實際上,它比預期容易! – davideghz