【问题标题】:How to add :format options to a named route in Rails?如何将 :format 选项添加到 Rails 中的命名路由?
【发布时间】:2010-03-13 14:40:50
【问题描述】:

我有一个名为 profile 的命名路由,我希望能够以 json 格式访问它。但是当我查看我的 rake 路由输出时,我发现 (.:format) 丢失了。如何将其添加到命名路由?

user GET    /users/:id(.:format)    {:action=>"show", :controller=>"users"}
profile     /:username              {:action=>"show", :controller=>"users"}

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby format named-routing


    【解决方案1】:

    在您的路径规范中添加.format,如下所示:

    map.profile '/:profile.:format', :controller => 'users', :action => 'show'
    

    然后你会得到:

    profile    /:profile(.:format)     {:action=>"show", :controller=>"users"}
    

    【讨论】:

    • 我试过 map.profile '/:profile(.:format)', :controller => 'users', :action => 'show' 很惊讶它没有用。我觉得应该! :-)
    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2021-07-09
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多