Rails 4 - explicit model name for resource id route

耗尽温柔 提交于 2019-12-06 11:56:15

I'm not sure if this is what you are looking for but, in your routes.rb,

If you use

resources :streams, param: :stream_id

Rails will generate the following urls:

          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

Hope this helps

UPDATE:

I'm not sure if this is best practices or not or if there is a better way but what if you try:

resources :streams, param: :stream_id

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

Rails would generate the following urls:

            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

Hope this helps

Try

resources :streams, :param => :stream_id

UPDATE:

What happens when you do this?

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

Similar to the post above but without the []: I used:

resources :tickets, param: :ticket_id

resources :tickets do
  resources :entries, param: :entry_id
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!