【问题标题】:Ruby on Rails optional route segmentsRuby on Rails 可选路线段
【发布时间】:2015-09-28 21:39:18
【问题描述】:

我正在尝试在我的 Rails 应用程序中使用可选参数创建两条路由。

这是我的路线文件:

get '(a)(/:area_id/)l/:location_id/(*url_title)', to: 'locations#show', as: :location
get 'a/:area_id/(*url_title)', to: 'areas#show', as: :area

导航到以下 URL 正确地将我路由到正确的控制器:

http://localhost:3000/a/1/l/2/seo-friendly-title.html
http://localhost:3000/a/1/seo-friendly-title.html

但是,导航到此 url 不起作用:

http://localhost:3000/l/2/seo-friendly-title.html

我收到No route matches 错误。当l/:location_id 部分存在时,是否可以将a/:area_id 部分设为可选?

使用Rails 4.2.4

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您可以添加另一条路线...

    get 'l/:location_id/(*url_title)', to: 'locations#show'
    

    两条路线没有理由不能映射到同一个动作。

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多