Rails - 使用“namespaced”URL

时间:2016-06-14 22:33:26

标签: ruby-on-rails routes

我有一个类似Trips( id:integer, from: string, to: string, date: date)的模型。

如何设置我的路线和trips_controller,以便能够查询我的数据库,只获取root_path/trips/:from/:to/:date等网址?

1 个答案:

答案 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