2014-02-27 119 views
2

當定義在routes.rb以下路線:軌道4 - 資源ID路線明確的型號名稱

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) 

你有一個想法如何解決這個問題??

回答

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 

希望這會有幫助

UPDATE:

我不知道這是否是最佳做法或沒有或是否有更好的辦法,但如果你嘗試:

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 

希望這有助於

+0

太好了,謝謝。剛剛編輯我的問題,因爲對於嵌套的資源,它變得有點混亂... – guyaloni

+1

@guyaloni,只是更新了我的答案,希望這有助於 – user2262149

+0

好吧,看起來不是最優雅的解決方案,但它的作品,謝謝! 「only:[]」是做什麼的? – guyaloni

2

嘗試

resources :streams, :param => :stream_id 

UPDATE:

什麼,當你這樣做會發生什麼?

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

感謝您的回答。請參閱我的問題的編輯部分嵌套資源。 – guyaloni

+0

@guyaloni:查看我的更新 – usha

+0

這個解決方案的問題是在內部對象的url(在這種情況下爲Comment)時,stream_id變成':stream_stream_id'。 – guyaloni

0

以上但沒有[]後相似: 我用:

resources :tickets, param: :ticket_id 

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