【问题标题】:Default segment name in rails resources routingrails 资源路由中的默认段名称
【发布时间】:2008-10-08 10:22:36
【问题描述】:

我想在我的 rails 应用程序中创建一条路线

/panda/blog
/tiger/blog
/dog/blog

其中 panda、tiger 和 dog 都是永久链接(对于动物类)

正常的做法

map.resources :animals do |animal|
 animal.resource :blog
end

将沿线创建路线

/animals/panda/blog
/animals/tiger/blog
/animals/dog/blog

但我不想要第一段,因为它总是一样的。

我知道我可以通过手动路由来做到这一点,但我想知道如何使用 rails 资源,因为我需要拥有动物和博客。

【问题讨论】:

    标签: ruby-on-rails resources routing


    【解决方案1】:

    在 rails 3.x 中,您可以将 path => "" 添加到任何 resourceresources 调用中,以从生成的路径中删除第一段。

    resources :animals, :path => ""
    

    $ rake routes
    
        animals GET    /                   {:action=>"index", :controller=>"animals"}
                POST   /                   {:action=>"create", :controller=>"animals"}
     new_animal GET    /new(.:format)      {:action=>"new", :controller=>"animals"}
    edit_animal GET    /:id/edit(.:format) {:action=>"edit", :controller=>"animals"}
         animal GET    /:id(.:format)      {:action=>"show", :controller=>"animals"}
                PUT    /:id(.:format)      {:action=>"update", :controller=>"animals"}
                DELETE /:id(.:format)      {:action=>"destroy", :controller=>"animals"}
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2014-12-24
        • 1970-01-01
        • 2015-09-26
        • 2011-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-12
        • 2014-03-31
        相关资源
        最近更新 更多