【问题标题】:How to add a custom route with limited resources如何添加资源有限的自定义路由
【发布时间】:2017-05-12 14:10:16
【问题描述】:

我的friendships 控制器一开始只需要两个动作,create 和destroy,所以我在config/routes.rb 中添加了以下内容:

resources :friendships, only: [:create, :destroy]

但是,我必须添加自定义 drop 操作以允许用户放弃友谊请求。 由于我只需要这三个动作,并且不会使用所有的 RESTful 资源,所以我想知道我是否可以将config/routes.rb 编辑如下:

resources :friendships, only: [:create, :destroy] do
  member do
    post :request, action: :drop
  end
end

【问题讨论】:

    标签: ruby-on-rails routes action


    【解决方案1】:

    是的,你可以;这样做会导致这条路线:

                Prefix Verb   URI Pattern                        Controller#Action
    request_friendship POST   /friendships/:id/request(.:format) friendships#drop
           friendships POST   /friendships(.:format)             friendships#create
            friendship DELETE /friendships/:id(.:format)         friendships#destroy
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2012-11-07
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多