Rails 路由:只有自定义操作的资源 [英] Rails routing: resources with only custom actions

查看:37
本文介绍了Rails 路由:只有自定义操作的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NotificationsController,其中我只有操作 clear.

I have a NotificationsController, in which I only have the action clear.

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

I'd like to access this action by doing POST /notifications/clear

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

So I wrote this in my router:

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

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

Is there a cleaner way to achieve this? I thought

  scope :notifications do
    post :clear
  end

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

would do it, but I have a missing controller error, because - I think - it looks for the clear controller.

推荐答案

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

If you are using scope, you should add a controller looks like

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

或者直接使用命名空间

namespace :notifications do
  post 'clear'
end

这篇关于Rails 路由:只有自定义操作的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆