我有一个类似Trips( id:integer, from: string, to: string, date: date)
的模型。
如何设置我的路线和trips_controller,以便能够查询我的数据库,只获取root_path/trips/:from/:to/:date
等网址?
答案 0 :(得分: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