2016-06-14 5 views
0

私はTrips(id:integer, from: string, to: string, date: date)のようなモデルを持っています。Rails - "namespaced" URLのquery db

root_path/trips/:from/:to/:dateのようなURLを取得するだけでDBを照会できるようにするには、ルートとtrips_controllerを設定するにはどうすればよいですか?

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

そして、あなたのコントローラは次のようになります:あなたのルートにこれを追加すること

+0

を、あなたは私たちにそのURLの例を示していることはできますか? – Wikiti

+0

root_path/trips/Boston/Chicago/2016-07-01 – davideghz

答えて

1

てみconfig/routes.rbファイル

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

awesome、それはうまく動作します:)実際には、予想より簡単でした! – davideghz