【问题标题】:Rails route 'stacking' (not sure what it's called)Rails 路线“堆叠”(不知道它叫什么)
【发布时间】:2014-01-31 04:38:40
【问题描述】:

这里有什么问题?:

get 'cars/index'                             <- works
get 'carsBLAH/index'                         <- Breaks!
get 'cars'        to: 'cars#index'           <- works

我认为这是某种轨道魔法/糖,但我找不到关于这种情况的任何信息。

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 routing routes rails-routing


    【解决方案1】:

    当你定义:

    get 'carsBLAH/index'
    

    默认情况下,Rails 在CarsBLAHsController 中查找index 操作。如果出现以下情况,它将中断:

    1. CarsBLAHsController 未定义。
    2. CarsBLAHsController 存在,但 index 方法未定义。

    您可以指定controlleraction 来执行路由:

    # executes CarsController#index
    get 'carsBLAH/index', to: 'cars#index'
    

    建议阅读Rails Routing from the Outside In了解详情。

    【讨论】:

    • 哦,我现在明白了……我认为在 routes.rb 中堆叠汽车和汽车/索引路线有什么特别之处。实际发生的是每一行都是独立的,而 cars/index 不需要to: 的唯一原因是因为 cars#index 恰好存在。非常感谢。
    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多