Rails 4 - 资源id路由的显式模型名称

时间:2014-02-27 22:21:51

标签: ruby-on-rails ruby-on-rails-4 routing

routes.rb中定义以下路线时:

resources :streams

Rails会生成以下网址:

        streams    GET    /streams(.:format)
                   POST   /streams(.:format)
        new_stream GET    /streams/new(.:format)
       edit_stream GET    /streams/:id/edit(.:format)
            stream GET    /streams/:id(.:format)
                   PATCH  /streams/:id(.:format)
                   PUT    /streams/:id(.:format)
                   DELETE /streams/:id(.:format)

我希望有一个明确的资源ID,即:stream_id而不是:id

修改

对于简单的资源,解决方案就像@ user2262149和@vimsha提​​到的那样:

resources :streams, :param => :stream_id

问题在于嵌套资源。如果我这样做:

resources :streams do
  resource :comment, :param => :comment_id
end

我会得到这条路线(没问题):

stream_comments   GET  streams/:stream_id/comments(.:format)

但另一方面,对于父资源(再次,:id而不是:stream_id):

streams   GET  streams/:id(.:format)

因此....
如果我尝试解决它,将:param => :stream_id添加到父资源:

resources :streams, :param => :stream_id do
  resource :comment, :param => :comment_id
end

然后对于父资源,路由是正确的:

stream GET    /api/streams/:stream_id(.:format)

但我的儿童资源真是一团糟:

stream_comments GET    /api/streams/:stream_stream_id/comments(.:format)

你知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

尝试

resources :streams, :param => :stream_id

更新:

当你这样做时会发生什么?

resources :streams, :param => :stream_id do
  resource :comment, :param => :comment_id
end

答案 1 :(得分:1)

我不确定这是否是您要找的,但是,在routes.rb

如果您使用

resources :streams, param: :stream_id

Rails将生成以下网址:

          streams GET      /streams(.:format)                       streams#index
                  POST     /streams(.:format)                       streams#create
       new_stream GET      /streams/new(.:format)                   streams#new
      edit_stream GET      /streams/:stream_id/edit(.:format)       streams#edit
           stream GET      /streams/:stream_id(.:format)            streams#show
                  PATCH    /streams/:stream_id(.:format)            streams#update
                  PUT      /streams/:stream_id(.:format)            streams#update
                  DELETE   /streams/:stream_id(.:format)            streams#destroy

希望这有帮助

更新:

我不确定这是否是最佳做法,或者是否有更好的方法,但如果你尝试的话:

resources :streams, param: :stream_id

resources :streams, only: [] do
  resource :comment, param: :comment_id
end

Rails会生成以下网址:

            streams GET      /streams(.:format)                         streams#index
                    POST     /streams(.:format)                         streams#create
         new_stream GET      /streams/new(.:format)                     streams#new
        edit_stream GET      /streams/:stream_id/edit(.:format)         streams#edit
             stream GET      /streams/:stream_id(.:format)              streams#show
                    PATCH    /streams/:stream_id(.:format)              streams#update
                    PUT      /streams/:stream_id(.:format)              streams#update
                    DELETE   /streams/:stream_id(.:format)              streams#destroy
     stream_comment POST     /streams/:stream_id/comment(.:format)      comments#create
 new_stream_comment GET      /streams/:stream_id/comment/new(.:format)  comments#new
edit_stream_comment GET      /streams/:stream_id/comment/edit(.:format) comments#edit
                    GET      /streams/:stream_id/comment(.:format)      comments#show
                    PATCH    /streams/:stream_id/comment(.:format)      comments#update
                    PUT      /streams/:stream_id/comment(.:format)      comments#update
                    DELETE   /streams/:stream_id/comment(.:format)      comments#destroy

希望这有帮助

答案 2 :(得分:0)

与上面的帖子相似但没有[]: 我用过:

resources :tickets, param: :ticket_id

resources :tickets do
  resources :entries, param: :entry_id
end