【问题标题】:Rails routing: resources with only custom actionsRails 路由:只有自定义操作的资源
【发布时间】:2013-07-19 10:18:28
【问题描述】:

我有一个NotificationsController,其中我只有clear 的动作。

我想通过执行 POST /notifications/clear 来访问此操作

所以我在路由器里写了这个:

  resources :notifications, :only => [] do
    collection do
      post :clear
    end
  end

有没有更简洁的方法来实现这一点?我以为

  scope :notifications do
    post :clear
  end

会这样做,但我有一个 missing controller 错误,因为 - 我认为 - 它会查找 clear 控制器。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rails-routing restful-architecture


    【解决方案1】:

    如果你使用范围,你应该添加一个控制器看起来像

    scope :notifications, :controller => 'notifications' do
      post 'clear'
    end
    

    或者只使用命名空间

    namespace :notifications do
      post 'clear'
    end
    

    【讨论】:

    • 如果我同时需要一个集合和一个成员呢?我还需要手动定义post '/:id/edit?
    • 在您的情况下并使用范围或命名空间,是的 (get :edit, :path => '/:id/edit') 。如果您不需要手动操作,则可以在您的问题上使用您所写的内容。阅读这里stackoverflow.com/q/17465335/1297435
    • 是的,我觉得:only => [] 是最明确且不那么老套的解决方案。谢谢
    • 我同意@AugustinRiedinger,只使用:[] 对我来说看起来更干净,然后你可以像往常一样使用会员和收藏
    【解决方案2】:
    post "notifications/clear" => "notifications#clear"
    

    【讨论】:

    • 请考虑解释为什么您的答案是正确的,以及这意味着什么。
    • 我想对所有通知操作进行分组,并在其中包含一个 method :action,就像资源一样,但没有我不使用的默认操作。
    【解决方案3】:
    1. 我尝试了三种组合:
    namespace :notifications do
      put :clear
    end
    
    scope :notifications, controller: :notifications do
      put :clear
    end
    
    resources :notifications, only: [] do
      collection do
        put :clear
      end
    end
    

    rails routes:

    notifications_clear   PUT /notifications/clear(.:format)  notifications#clear
    clear                 PUT /notifications/clear(.:format)  notifications#clear
    clear_notifications   PUT /notifications/clear(.:format)  notifications#clear # I choose this
    

    因为 url helper clear_notifications_*,我会选择第三种组合。


    1. 此外,实际上我想将这些路由嵌套在资源中:
    resources :users do
      namespace :notifications do
        put :clear
      end
    
      scope :notifications, controller: :notifications do
        put :clear
      end
    
      resources :notifications, only: [] do
        collection do
          put :clear
        end
      end
    end
    

    rails routes:

    user_notifications_clear  PUT /users/:user_id/notifications/clear(.:format)  notifications/users#clear
    user_clear                PUT /notifications/users/:user_id/clear(.:format)  notifications#clear
    clear_user_notifications  PUT /users/:user_id/notifications/clear(.:format)  notifications#clear
    

    最好将resources 块与only: [] 一起使用。


    附:我认为通过命名用户下的通知控制器更有意义:

    resources :users, module: :users do
      resources :notifications, only: [] do
        collection do
          put :clear
        end
      end
    end
    
    clear_user_notifications  PUT /users/:user_id/notifications/clear(.:format)  users/notifications#clear
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2015-09-26
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多